github.com/minio/madmin-go/v2@v2.2.1/examples/service-account.go (about)

     1  //
     2  //go:build ignore
     3  // +build ignore
     4  
     5  //
     6  // Copyright (c) 2015-2022 MinIO, Inc.
     7  //
     8  // This file is part of MinIO Object Storage stack
     9  //
    10  // This program is free software: you can redistribute it and/or modify
    11  // it under the terms of the GNU Affero General Public License as
    12  // published by the Free Software Foundation, either version 3 of the
    13  // License, or (at your option) any later version.
    14  //
    15  // This program is distributed in the hope that it will be useful,
    16  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    17  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    18  // GNU Affero General Public License for more details.
    19  //
    20  // You should have received a copy of the GNU Affero General Public License
    21  // along with this program. If not, see <http://www.gnu.org/licenses/>.
    22  //
    23  
    24  package main
    25  
    26  import (
    27  	"context"
    28  	"fmt"
    29  	"log"
    30  	"time"
    31  
    32  	"github.com/minio/madmin-go/v2"
    33  )
    34  
    35  func main() {
    36  	// Note: YOUR-ACCESSKEYID, YOUR-SECRETACCESSKEY and my-bucketname are
    37  	// dummy values, please replace them with original values.
    38  
    39  	// API requests are secure (HTTPS) if secure=true and insecure (HTTP) otherwise.
    40  	// New returns an MinIO Admin client object.
    41  	madminClient, err := madmin.New("your-minio.example.com:9000", "YOUR-ACCESSKEYID", "YOUR-SECRETACCESSKEY", true)
    42  	if err != nil {
    43  		log.Fatalln(err)
    44  	}
    45  	ctx := context.Background()
    46  
    47  	// add service account
    48  	expiration := time.Now().Add(30 * time.Minute)
    49  	addReq := madmin.AddServiceAccountReq{
    50  		TargetUser: "my-username",
    51  		Expiration: &expiration,
    52  	}
    53  	addRes, err := madminClient.AddServiceAccount(context.Background(), addReq)
    54  	if err != nil {
    55  		log.Fatalln(err)
    56  	}
    57  	fmt.Println(addRes)
    58  
    59  	// update service account
    60  	newExpiration := time.Now().Add(45 * time.Minute)
    61  	updateReq := madmin.UpdateServiceAccountReq{
    62  		NewStatus:     "my-status",
    63  		NewExpiration: &newExpiration,
    64  	}
    65  	if err := madminClient.UpdateServiceAccount(ctx, "my-accesskey", updateReq); err != nil {
    66  		log.Fatalln(err)
    67  	}
    68  
    69  	// get service account
    70  	listRes, err := madminClient.ListServiceAccounts(ctx, "my-username")
    71  	if err != nil {
    72  		log.Fatalln(err)
    73  	}
    74  	fmt.Println(listRes)
    75  
    76  	// delete service account
    77  	if err := madminClient.DeleteServiceAccount(ctx, "my-accesskey"); err != nil {
    78  		log.Fatalln(err)
    79  	}
    80  }