github.com/minio/madmin-go/v2@v2.2.1/group-commands.go (about)

     1  //
     2  // Copyright (c) 2015-2022 MinIO, Inc.
     3  //
     4  // This file is part of MinIO Object Storage stack
     5  //
     6  // This program is free software: you can redistribute it and/or modify
     7  // it under the terms of the GNU Affero General Public License as
     8  // published by the Free Software Foundation, either version 3 of the
     9  // License, or (at your option) any later version.
    10  //
    11  // This program is distributed in the hope that it will be useful,
    12  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  // GNU Affero General Public License for more details.
    15  //
    16  // You should have received a copy of the GNU Affero General Public License
    17  // along with this program. If not, see <http://www.gnu.org/licenses/>.
    18  //
    19  
    20  package madmin
    21  
    22  import (
    23  	"context"
    24  	"encoding/json"
    25  	"io/ioutil"
    26  	"net/http"
    27  	"net/url"
    28  	"time"
    29  )
    30  
    31  // GroupAddRemove is type for adding/removing members to/from a group.
    32  type GroupAddRemove struct {
    33  	Group    string      `json:"group"`
    34  	Members  []string    `json:"members"`
    35  	Status   GroupStatus `json:"groupStatus"`
    36  	IsRemove bool        `json:"isRemove"`
    37  }
    38  
    39  // UpdateGroupMembers - adds/removes users to/from a group. Server
    40  // creates the group as needed. Group is removed if remove request is
    41  // made on empty group.
    42  func (adm *AdminClient) UpdateGroupMembers(ctx context.Context, g GroupAddRemove) error {
    43  	data, err := json.Marshal(g)
    44  	if err != nil {
    45  		return err
    46  	}
    47  
    48  	reqData := requestData{
    49  		relPath: adminAPIPrefix + "/update-group-members",
    50  		content: data,
    51  	}
    52  
    53  	// Execute PUT on /minio/admin/v3/update-group-members
    54  	resp, err := adm.executeMethod(ctx, http.MethodPut, reqData)
    55  
    56  	defer closeResponse(resp)
    57  	if err != nil {
    58  		return err
    59  	}
    60  
    61  	if resp.StatusCode != http.StatusOK {
    62  		return httpRespToErrorResponse(resp)
    63  	}
    64  
    65  	return nil
    66  }
    67  
    68  // GroupDesc is a type that holds group info along with the policy
    69  // attached to it.
    70  type GroupDesc struct {
    71  	Name      string    `json:"name"`
    72  	Status    string    `json:"status"`
    73  	Members   []string  `json:"members"`
    74  	Policy    string    `json:"policy"`
    75  	UpdatedAt time.Time `json:"updatedAt,omitempty"`
    76  }
    77  
    78  // GetGroupDescription - fetches information on a group.
    79  func (adm *AdminClient) GetGroupDescription(ctx context.Context, group string) (*GroupDesc, error) {
    80  	v := url.Values{}
    81  	v.Set("group", group)
    82  	reqData := requestData{
    83  		relPath:     adminAPIPrefix + "/group",
    84  		queryValues: v,
    85  	}
    86  
    87  	resp, err := adm.executeMethod(ctx, http.MethodGet, reqData)
    88  	defer closeResponse(resp)
    89  	if err != nil {
    90  		return nil, err
    91  	}
    92  
    93  	if resp.StatusCode != http.StatusOK {
    94  		return nil, httpRespToErrorResponse(resp)
    95  	}
    96  
    97  	data, err := ioutil.ReadAll(resp.Body)
    98  	if err != nil {
    99  		return nil, err
   100  	}
   101  
   102  	gd := GroupDesc{}
   103  	if err = json.Unmarshal(data, &gd); err != nil {
   104  		return nil, err
   105  	}
   106  
   107  	return &gd, nil
   108  }
   109  
   110  // ListGroups - lists all groups names present on the server.
   111  func (adm *AdminClient) ListGroups(ctx context.Context) ([]string, error) {
   112  	reqData := requestData{
   113  		relPath: adminAPIPrefix + "/groups",
   114  	}
   115  
   116  	resp, err := adm.executeMethod(ctx, http.MethodGet, reqData)
   117  	defer closeResponse(resp)
   118  	if err != nil {
   119  		return nil, err
   120  	}
   121  
   122  	if resp.StatusCode != http.StatusOK {
   123  		return nil, httpRespToErrorResponse(resp)
   124  	}
   125  
   126  	data, err := ioutil.ReadAll(resp.Body)
   127  	if err != nil {
   128  		return nil, err
   129  	}
   130  
   131  	groups := []string{}
   132  	if err = json.Unmarshal(data, &groups); err != nil {
   133  		return nil, err
   134  	}
   135  
   136  	return groups, nil
   137  }
   138  
   139  // GroupStatus - group status.
   140  type GroupStatus string
   141  
   142  // GroupStatus values.
   143  const (
   144  	GroupEnabled  GroupStatus = "enabled"
   145  	GroupDisabled GroupStatus = "disabled"
   146  )
   147  
   148  // SetGroupStatus - sets the status of a group.
   149  func (adm *AdminClient) SetGroupStatus(ctx context.Context, group string, status GroupStatus) error {
   150  	v := url.Values{}
   151  	v.Set("group", group)
   152  	v.Set("status", string(status))
   153  
   154  	reqData := requestData{
   155  		relPath:     adminAPIPrefix + "/set-group-status",
   156  		queryValues: v,
   157  	}
   158  
   159  	resp, err := adm.executeMethod(ctx, http.MethodPut, reqData)
   160  	defer closeResponse(resp)
   161  	if err != nil {
   162  		return err
   163  	}
   164  
   165  	if resp.StatusCode != http.StatusOK {
   166  		return httpRespToErrorResponse(resp)
   167  	}
   168  
   169  	return nil
   170  }