...
1 package iam
2
3 import (
4 "context"
5
6 "google.golang.org/api/googleapi"
7 "google.golang.org/api/iam/v1"
8 )
9
10
11 func NewServiceAccount(displayName, description string) *iam.ServiceAccount {
12 return &iam.ServiceAccount{
13 DisplayName: displayName,
14 Description: description,
15 }
16 }
17
18
19 func NewServiceAccountRequest(accountID string, serviceAccount *iam.ServiceAccount) *iam.CreateServiceAccountRequest {
20 return &iam.CreateServiceAccountRequest{
21 AccountId: accountID,
22 ServiceAccount: serviceAccount,
23 }
24 }
25
26
27 func (i *IAMService) CreateServiceAccount(ctx context.Context, projectID string, serviceAccount *iam.CreateServiceAccountRequest) (*iam.ServiceAccount, error) {
28 return i.Projects.ServiceAccounts.Create(projectID, serviceAccount).Context(ctx).Do()
29 }
30
31
32
33
34 func (i *IAMService) DeleteServiceAccount(ctx context.Context, name string) error {
35 _, err := i.Projects.ServiceAccounts.Delete(name).Context(ctx).Do()
36 return err
37 }
38
39
40
41 func (i *IAMService) GetServiceAccount(ctx context.Context, name string) (*iam.ServiceAccount, error) {
42 return i.Projects.ServiceAccounts.Get(name).Context(ctx).Do()
43 }
44
45
46 func NewServiceAccountKeyRequest() *iam.CreateServiceAccountKeyRequest {
47 return &iam.CreateServiceAccountKeyRequest{}
48 }
49
50
51 func (i *IAMService) CreateServiceAccountKey(ctx context.Context, name string, saKeyRequest *iam.CreateServiceAccountKeyRequest, opts ...googleapi.CallOption) (*iam.ServiceAccountKey, error) {
52 return i.Projects.ServiceAccounts.Keys.Create(name, saKeyRequest).Context(ctx).Do(opts...)
53 }
54
55
56
57 func (i *IAMService) DeleteExcessServiceAccountKeys(ctx context.Context, name string, opts ...googleapi.CallOption) error {
58 accs, err := i.Projects.ServiceAccounts.Keys.List(name).Context(ctx).Do(opts...)
59 if err != nil {
60 return err
61 }
62 if len(accs.Keys) < 10 {
63 return nil
64 }
65 for _, acc := range accs.Keys {
66 if acc.KeyType == "SYSTEM_MANAGED" {
67 continue
68 }
69 _, err = i.Projects.ServiceAccounts.Keys.Delete(acc.Name).Context(ctx).Do(opts...)
70 if err != nil {
71 return err
72 }
73 }
74 return nil
75 }
76
77
78 func (i *IAMService) GetServiceAccountIamPolicy(ctx context.Context, resource string) (*iam.Policy, error) {
79 return i.Projects.ServiceAccounts.GetIamPolicy(resource).Context(ctx).Do()
80 }
81
82
83 func (i *IAMService) SetServiceAccountIamPolicy(ctx context.Context, resource string, policyRequest *iam.SetIamPolicyRequest) (*iam.Policy, error) {
84 return i.Projects.ServiceAccounts.SetIamPolicy(resource, policyRequest).Context(ctx).Do()
85 }
86
View as plain text