github.com/jfrog/jfrog-cli-core/v2@v2.51.0/artifactory/commands/permissiontarget/permissiontarget.go (about)

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