github.com/minio/madmin-go@v1.7.5/examples/service-account.go (about)

     1  //go:build ignore
     2  // +build ignore
     3  //
     4  // MinIO Object Storage (c) 2022 MinIO, Inc.
     5  //
     6  // Licensed under the Apache License, Version 2.0 (the "License");
     7  // you may not use this file except in compliance with the License.
     8  // You may obtain a copy of the License at
     9  //
    10  //      http://www.apache.org/licenses/LICENSE-2.0
    11  //
    12  // Unless required by applicable law or agreed to in writing, software
    13  // distributed under the License is distributed on an "AS IS" BASIS,
    14  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    15  // See the License for the specific language governing permissions and
    16  // limitations under the License.
    17  //
    18  
    19  package main
    20  
    21  import (
    22  	"context"
    23  	"fmt"
    24  	"log"
    25  
    26  	"github.com/minio/madmin-go"
    27  )
    28  
    29  func main() {
    30  	// Note: YOUR-ACCESSKEYID, YOUR-SECRETACCESSKEY and my-bucketname are
    31  	// dummy values, please replace them with original values.
    32  
    33  	// API requests are secure (HTTPS) if secure=true and insecure (HTTP) otherwise.
    34  	// New returns an MinIO Admin client object.
    35  	madminClient, err := madmin.New("your-minio.example.com:9000", "YOUR-ACCESSKEYID", "YOUR-SECRETACCESSKEY", true)
    36  	if err != nil {
    37  		log.Fatalln(err)
    38  	}
    39  	ctx := context.Background()
    40  	
    41  	// add service account
    42  	addReq := madmin.AddServiceAccountReq{
    43  		TargetUser: "my-username",
    44  	}
    45  	addRes, err := madminClient.AddServiceAccount(context.Background(), addReq)
    46  	if err != nil {
    47  		log.Fatalln(err)
    48  	}
    49  	fmt.Println(addRes)
    50  
    51  	// update service account
    52  	updateReq := madmin.UpdateServiceAccountReq{
    53  		NewStatus: "my-status",
    54  	}
    55  	if err := madminClient.UpdateServiceAccount(ctx, "my-accesskey", updateReq); err != nil {
    56  		log.Fatalln(err)
    57  	}
    58  
    59  	// get service account
    60  	listRes, err := madminClient.ListServiceAccounts(ctx, "my-username")
    61  	if err != nil {
    62  		log.Fatalln(err)
    63  	}
    64  	fmt.Println(listRes)
    65  
    66  	// delete service account
    67  	if err := madminClient.DeleteServiceAccount(ctx, "my-accesskey"); err != nil {
    68  		log.Fatalln(err)
    69  	}
    70  }