-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
UpdateUser.go
62 lines (53 loc) · 1.88 KB
/
UpdateUser.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MIT-0
// snippet-start:[iam.go.update_user]
package main
// snippet-start:[iam.go.update_user.imports]
import (
"flag"
"fmt"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/iam"
"github.com/aws/aws-sdk-go/service/iam/iamiface"
)
// snippet-end:[iam.go.update_user.imports]
// RenameUser changes the name for an IAM user.
// Inputs:
// sess is the current session, which provides configuration for the SDK's service clients
// userName is the name of the user
// newName is the new user name
// Output:
// If success, nil
// Otherwise, an error from the call to CreateUser
func RenameUser(svc iamiface.IAMAPI, userName, newName *string) error {
// snippet-start:[iam.go.update_user.call]
_, err := svc.UpdateUser(&iam.UpdateUserInput{
UserName: userName,
NewUserName: newName,
})
// snippet-end:[iam.go.update_user.call]
return err
}
func main() {
// snippet-start:[iam.go.update_user.args]
userName := flag.String("u", "", "The name of the user")
newName := flag.String("n", "", "The new name of the user")
flag.Parse()
if *userName == "" || *newName == "" {
fmt.Println("You must supply a user name and new name (-u USERNAME -n NEW-NAME)")
return
}
// snippet-end:[iam.go.update_user.args]
// snippet-start:[iam.go.update_user.session]
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
svc := iam.New(sess)
// snippet-end:[iam.go.update_user.session]
err := RenameUser(svc, userName, newName)
if err != nil {
fmt.Println("Got an error updating user " + *userName)
}
fmt.Println("Updated user " + *userName)
}
// snippet-end:[iam.go.update_user]