github.com/osievert/jfrog-cli-core@v1.2.7/artifactory/commands/permissiontarget/permissiontarget.go (about)

     1  package permissiontarget
     2  
     3  import (
     4  	"encoding/json"
     5  	"errors"
     6  	"github.com/jfrog/jfrog-cli-core/artifactory/commands/utils"
     7  	rtUtils "github.com/jfrog/jfrog-cli-core/artifactory/utils"
     8  	"github.com/jfrog/jfrog-cli-core/utils/config"
     9  	"github.com/jfrog/jfrog-client-go/artifactory/services"
    10  	"github.com/jfrog/jfrog-client-go/utils/errorutils"
    11  	"strings"
    12  )
    13  
    14  const DefaultBuildRepositoriesValue = "artifactory-build-info"
    15  
    16  type PermissionTargetCommand struct {
    17  	rtDetails    *config.ArtifactoryDetails
    18  	templatePath string
    19  	vars         string
    20  }
    21  
    22  func (ptc *PermissionTargetCommand) Vars() string {
    23  	return ptc.vars
    24  }
    25  
    26  func (ptc *PermissionTargetCommand) TemplatePath() string {
    27  	return ptc.templatePath
    28  }
    29  
    30  func (ptc *PermissionTargetCommand) PerformPermissionTargetCmd(isUpdate bool) (err error) {
    31  	permissionTargetConfigMap, err := utils.ConvertTemplateToMap(ptc)
    32  	if err != nil {
    33  		return err
    34  	}
    35  	// Go over the the confMap and write the values with the correct types
    36  	for key, value := range permissionTargetConfigMap {
    37  		isBuildSection := false
    38  		switch key {
    39  		case Name:
    40  			if _, ok := value.(string); !ok {
    41  				return errorutils.CheckError(errors.New("template syntax error: the value for the  key: \"Name\" is not a string type."))
    42  			}
    43  		case Build:
    44  			isBuildSection = true
    45  			fallthrough
    46  		case Repo:
    47  			fallthrough
    48  		case ReleaseBundle:
    49  			permissionSection, err := covertPermissionSection(value, isBuildSection)
    50  			if err != nil {
    51  				return err
    52  			}
    53  			permissionTargetConfigMap[key] = permissionSection
    54  		default:
    55  			return errorutils.CheckError(errors.New("template syntax error: unknown key: \"" + key + "\"."))
    56  		}
    57  	}
    58  	// Convert the new JSON with the correct types to params struct
    59  	content, err := json.Marshal(permissionTargetConfigMap)
    60  	params := services.NewPermissionTargetParams()
    61  	err = json.Unmarshal(content, &params)
    62  	if errorutils.CheckError(err) != nil {
    63  		return err
    64  	}
    65  	servicesManager, err := rtUtils.CreateServiceManager(ptc.rtDetails, false)
    66  	if err != nil {
    67  		return err
    68  	}
    69  	if isUpdate {
    70  		return servicesManager.UpdatePermissionTarget(params)
    71  	}
    72  	return servicesManager.CreatePermissionTarget(params)
    73  }
    74  
    75  // Each section is a map of string->interface{}. We need to convert each value to its correct type
    76  func covertPermissionSection(value interface{}, isBuildSection bool) (*services.PermissionTargetSection, error) {
    77  	content, err := json.Marshal(value)
    78  	if errorutils.CheckError(err) != nil {
    79  		return nil, err
    80  	}
    81  	var answer PermissionSectionAnswer
    82  	err = json.Unmarshal(content, &answer)
    83  	if errorutils.CheckError(err) != nil {
    84  		return nil, err
    85  	}
    86  	var pts services.PermissionTargetSection
    87  	if len(answer.IncludePatterns) > 0 {
    88  		pts.IncludePatterns = strings.Split(answer.IncludePatterns, ",")
    89  	}
    90  	if len(answer.ExcludePatterns) > 0 {
    91  		pts.ExcludePatterns = strings.Split(answer.ExcludePatterns, ",")
    92  	}
    93  	// 'build' permission target must include repositories with a default value that cannot be changed.
    94  	if isBuildSection {
    95  		answer.Repositories = DefaultBuildRepositoriesValue
    96  	}
    97  	if len(answer.Repositories) > 0 {
    98  		pts.Repositories = strings.Split(answer.Repositories, ",")
    99  	}
   100  	if answer.ActionsUsers != nil {
   101  		convertActionMap(answer.ActionsUsers, &pts.Actions.Users)
   102  	}
   103  	if answer.ActionsGroups != nil {
   104  		convertActionMap(answer.ActionsGroups, &pts.Actions.Groups)
   105  	}
   106  	return &pts, nil
   107  }
   108  
   109  // actionMap is map of string->string. We need to convert each value to []string
   110  func convertActionMap(srcMap map[string]string, tgtMap *map[string][]string) {
   111  	*tgtMap = make(map[string][]string)
   112  	for key, permissionsStr := range srcMap {
   113  		(*tgtMap)[key] = strings.Split(permissionsStr, ",")
   114  	}
   115  
   116  }