github.com/cobalt77/jfrog-client-go@v0.14.5/artifactory/services/permissiontarget.go (about) 1 package services 2 3 import ( 4 "encoding/json" 5 "errors" 6 "net/http" 7 8 "github.com/cobalt77/jfrog-client-go/artifactory/services/utils" 9 10 rthttpclient "github.com/cobalt77/jfrog-client-go/artifactory/httpclient" 11 "github.com/cobalt77/jfrog-client-go/auth" 12 clientutils "github.com/cobalt77/jfrog-client-go/utils" 13 "github.com/cobalt77/jfrog-client-go/utils/errorutils" 14 "github.com/cobalt77/jfrog-client-go/utils/log" 15 ) 16 17 type PermissionTargetService struct { 18 client *rthttpclient.ArtifactoryHttpClient 19 ArtDetails auth.ServiceDetails 20 } 21 22 func NewPermissionTargetService(client *rthttpclient.ArtifactoryHttpClient) *PermissionTargetService { 23 return &PermissionTargetService{client: client} 24 } 25 26 func (pts *PermissionTargetService) GetJfrogHttpClient() *rthttpclient.ArtifactoryHttpClient { 27 return pts.client 28 } 29 30 func (pts *PermissionTargetService) Delete(permissionTargetName string) error { 31 httpClientsDetails := pts.ArtDetails.CreateHttpClientDetails() 32 log.Info("Deleting permission target...") 33 resp, body, err := pts.client.SendDelete(pts.ArtDetails.GetUrl()+"api/security/permissions/"+permissionTargetName, nil, &httpClientsDetails) 34 if err != nil { 35 return err 36 } 37 if resp.StatusCode != http.StatusOK { 38 return errorutils.CheckError(errors.New("Artifactory response: " + resp.Status + "\n" + clientutils.IndentJson(body))) 39 } 40 41 log.Debug("Artifactory response:", resp.Status) 42 log.Info("Done deleting permission target.") 43 return nil 44 } 45 46 func (pts *PermissionTargetService) Create(params PermissionTargetParams) error { 47 return pts.performRequest(params, false) 48 } 49 50 func (pts *PermissionTargetService) Update(params PermissionTargetParams) error { 51 return pts.performRequest(params, true) 52 } 53 54 func (pts *PermissionTargetService) performRequest(params PermissionTargetParams, update bool) error { 55 content, err := json.Marshal(params) 56 if err != nil { 57 return errorutils.CheckError(err) 58 } 59 httpClientsDetails := pts.ArtDetails.CreateHttpClientDetails() 60 utils.SetContentType("application/json", &httpClientsDetails.Headers) 61 var url = pts.ArtDetails.GetUrl() + "api/v2/security/permissions/" + params.Name 62 var operationString string 63 var resp *http.Response 64 var body []byte 65 if update { 66 log.Info("Updating permission target...") 67 operationString = "updating" 68 resp, body, err = pts.client.SendPut(url, content, &httpClientsDetails) 69 } else { 70 log.Info("Creating permission target...") 71 operationString = "creating" 72 resp, body, err = pts.client.SendPost(url, content, &httpClientsDetails) 73 } 74 if err != nil { 75 return err 76 } 77 if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated { 78 return errorutils.CheckError(errors.New("Artifactory response: " + resp.Status + "\n" + clientutils.IndentJson(body))) 79 } 80 log.Debug("Artifactory response:", resp.Status) 81 log.Info("Done " + operationString + " permission target.") 82 return nil 83 } 84 85 func NewPermissionTargetParams() PermissionTargetParams { 86 return PermissionTargetParams{} 87 } 88 89 type PermissionTargetParams struct { 90 Name string `json:"name"` 91 Repo PermissionTargetSection `json:"repo,omitempty"` 92 Build PermissionTargetSection `json:"build,omitempty"` 93 ReleaseBundle PermissionTargetSection `json:"releaseBundle,omitempty"` 94 } 95 96 type PermissionTargetSection struct { 97 IncludePatterns []string `json:"include-patterns,omitempty"` 98 ExcludePatterns []string `json:"exclude-patterns,omitempty"` 99 Repositories []string `json:"repositories"` 100 Actions Actions `json:"actions,omitempty"` 101 } 102 103 type Actions struct { 104 Users map[string][]string `json:"users,omitempty"` 105 Groups map[string][]string `json:"groups,omitempty"` 106 }