storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/pkg/madmin/group-commands.go (about) 1 /* 2 * MinIO Cloud Storage, (C) 2018-2019 MinIO, Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 * 16 */ 17 18 package madmin 19 20 import ( 21 "context" 22 "encoding/json" 23 "io/ioutil" 24 "net/http" 25 "net/url" 26 ) 27 28 // GroupAddRemove is type for adding/removing members to/from a group. 29 type GroupAddRemove struct { 30 Group string `json:"group"` 31 Members []string `json:"members"` 32 IsRemove bool `json:"isRemove"` 33 } 34 35 // UpdateGroupMembers - adds/removes users to/from a group. Server 36 // creates the group as needed. Group is removed if remove request is 37 // made on empty group. 38 func (adm *AdminClient) UpdateGroupMembers(ctx context.Context, g GroupAddRemove) error { 39 data, err := json.Marshal(g) 40 if err != nil { 41 return err 42 } 43 44 reqData := requestData{ 45 relPath: adminAPIPrefix + "/update-group-members", 46 content: data, 47 } 48 49 // Execute PUT on /minio/admin/v3/update-group-members 50 resp, err := adm.executeMethod(ctx, http.MethodPut, reqData) 51 52 defer closeResponse(resp) 53 if err != nil { 54 return err 55 } 56 57 if resp.StatusCode != http.StatusOK { 58 return httpRespToErrorResponse(resp) 59 } 60 61 return nil 62 } 63 64 // GroupDesc is a type that holds group info along with the policy 65 // attached to it. 66 type GroupDesc struct { 67 Name string `json:"name"` 68 Status string `json:"status"` 69 Members []string `json:"members"` 70 Policy string `json:"policy"` 71 } 72 73 // GetGroupDescription - fetches information on a group. 74 func (adm *AdminClient) GetGroupDescription(ctx context.Context, group string) (*GroupDesc, error) { 75 v := url.Values{} 76 v.Set("group", group) 77 reqData := requestData{ 78 relPath: adminAPIPrefix + "/group", 79 queryValues: v, 80 } 81 82 resp, err := adm.executeMethod(ctx, http.MethodGet, reqData) 83 defer closeResponse(resp) 84 if err != nil { 85 return nil, err 86 } 87 88 if resp.StatusCode != http.StatusOK { 89 return nil, httpRespToErrorResponse(resp) 90 } 91 92 data, err := ioutil.ReadAll(resp.Body) 93 if err != nil { 94 return nil, err 95 } 96 97 gd := GroupDesc{} 98 if err = json.Unmarshal(data, &gd); err != nil { 99 return nil, err 100 } 101 102 return &gd, nil 103 } 104 105 // ListGroups - lists all groups names present on the server. 106 func (adm *AdminClient) ListGroups(ctx context.Context) ([]string, error) { 107 reqData := requestData{ 108 relPath: adminAPIPrefix + "/groups", 109 } 110 111 resp, err := adm.executeMethod(ctx, http.MethodGet, reqData) 112 defer closeResponse(resp) 113 if err != nil { 114 return nil, err 115 } 116 117 if resp.StatusCode != http.StatusOK { 118 return nil, httpRespToErrorResponse(resp) 119 } 120 121 data, err := ioutil.ReadAll(resp.Body) 122 if err != nil { 123 return nil, err 124 } 125 126 groups := []string{} 127 if err = json.Unmarshal(data, &groups); err != nil { 128 return nil, err 129 } 130 131 return groups, nil 132 } 133 134 // GroupStatus - group status. 135 type GroupStatus string 136 137 // GroupStatus values. 138 const ( 139 GroupEnabled GroupStatus = "enabled" 140 GroupDisabled GroupStatus = "disabled" 141 ) 142 143 // SetGroupStatus - sets the status of a group. 144 func (adm *AdminClient) SetGroupStatus(ctx context.Context, group string, status GroupStatus) error { 145 v := url.Values{} 146 v.Set("group", group) 147 v.Set("status", string(status)) 148 149 reqData := requestData{ 150 relPath: adminAPIPrefix + "/set-group-status", 151 queryValues: v, 152 } 153 154 resp, err := adm.executeMethod(ctx, http.MethodPut, reqData) 155 defer closeResponse(resp) 156 if err != nil { 157 return err 158 } 159 160 if resp.StatusCode != http.StatusOK { 161 return httpRespToErrorResponse(resp) 162 } 163 164 return nil 165 }