github.com/minio/madmin-go@v1.7.5/group-commands.go (about) 1 // 2 // MinIO Object Storage (c) 2021 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 package madmin 18 19 import ( 20 "context" 21 "encoding/json" 22 "io/ioutil" 23 "net/http" 24 "net/url" 25 "time" 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 Status GroupStatus `json:"groupStatus"` 33 IsRemove bool `json:"isRemove"` 34 } 35 36 // UpdateGroupMembers - adds/removes users to/from a group. Server 37 // creates the group as needed. Group is removed if remove request is 38 // made on empty group. 39 func (adm *AdminClient) UpdateGroupMembers(ctx context.Context, g GroupAddRemove) error { 40 data, err := json.Marshal(g) 41 if err != nil { 42 return err 43 } 44 45 reqData := requestData{ 46 relPath: adminAPIPrefix + "/update-group-members", 47 content: data, 48 } 49 50 // Execute PUT on /minio/admin/v3/update-group-members 51 resp, err := adm.executeMethod(ctx, http.MethodPut, reqData) 52 53 defer closeResponse(resp) 54 if err != nil { 55 return err 56 } 57 58 if resp.StatusCode != http.StatusOK { 59 return httpRespToErrorResponse(resp) 60 } 61 62 return nil 63 } 64 65 // GroupDesc is a type that holds group info along with the policy 66 // attached to it. 67 type GroupDesc struct { 68 Name string `json:"name"` 69 Status string `json:"status"` 70 Members []string `json:"members"` 71 Policy string `json:"policy"` 72 UpdatedAt time.Time `json:"updatedAt,omitempty"` 73 } 74 75 // GetGroupDescription - fetches information on a group. 76 func (adm *AdminClient) GetGroupDescription(ctx context.Context, group string) (*GroupDesc, error) { 77 v := url.Values{} 78 v.Set("group", group) 79 reqData := requestData{ 80 relPath: adminAPIPrefix + "/group", 81 queryValues: v, 82 } 83 84 resp, err := adm.executeMethod(ctx, http.MethodGet, reqData) 85 defer closeResponse(resp) 86 if err != nil { 87 return nil, err 88 } 89 90 if resp.StatusCode != http.StatusOK { 91 return nil, httpRespToErrorResponse(resp) 92 } 93 94 data, err := ioutil.ReadAll(resp.Body) 95 if err != nil { 96 return nil, err 97 } 98 99 gd := GroupDesc{} 100 if err = json.Unmarshal(data, &gd); err != nil { 101 return nil, err 102 } 103 104 return &gd, nil 105 } 106 107 // ListGroups - lists all groups names present on the server. 108 func (adm *AdminClient) ListGroups(ctx context.Context) ([]string, error) { 109 reqData := requestData{ 110 relPath: adminAPIPrefix + "/groups", 111 } 112 113 resp, err := adm.executeMethod(ctx, http.MethodGet, reqData) 114 defer closeResponse(resp) 115 if err != nil { 116 return nil, err 117 } 118 119 if resp.StatusCode != http.StatusOK { 120 return nil, httpRespToErrorResponse(resp) 121 } 122 123 data, err := ioutil.ReadAll(resp.Body) 124 if err != nil { 125 return nil, err 126 } 127 128 groups := []string{} 129 if err = json.Unmarshal(data, &groups); err != nil { 130 return nil, err 131 } 132 133 return groups, nil 134 } 135 136 // GroupStatus - group status. 137 type GroupStatus string 138 139 // GroupStatus values. 140 const ( 141 GroupEnabled GroupStatus = "enabled" 142 GroupDisabled GroupStatus = "disabled" 143 ) 144 145 // SetGroupStatus - sets the status of a group. 146 func (adm *AdminClient) SetGroupStatus(ctx context.Context, group string, status GroupStatus) error { 147 v := url.Values{} 148 v.Set("group", group) 149 v.Set("status", string(status)) 150 151 reqData := requestData{ 152 relPath: adminAPIPrefix + "/set-group-status", 153 queryValues: v, 154 } 155 156 resp, err := adm.executeMethod(ctx, http.MethodPut, reqData) 157 defer closeResponse(resp) 158 if err != nil { 159 return err 160 } 161 162 if resp.StatusCode != http.StatusOK { 163 return httpRespToErrorResponse(resp) 164 } 165 166 return nil 167 }