-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathiam_resource.go
53 lines (44 loc) · 1.3 KB
/
iam_resource.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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package iamutil
import (
"context"
"encoding/json"
"fmt"
"strings"
"github.com/hashicorp/errwrap"
"github.com/hashicorp/go-gcp-common/gcputil"
)
// IamResource implements Resource.
type IamResource struct {
relativeId *gcputil.RelativeResourceName
config *RestResource
}
func (r *IamResource) GetConfig() *RestResource {
return r.config
}
func (r *IamResource) GetRelativeId() *gcputil.RelativeResourceName {
return r.relativeId
}
func (r *IamResource) GetIamPolicy(ctx context.Context, h *ApiHandle) (*Policy, error) {
var p Policy
if err := h.DoGetRequest(ctx, r, &p); err != nil {
return nil, errwrap.Wrapf("unable to get policy: {{err}}", err)
}
return &p, nil
}
func (r *IamResource) SetIamPolicy(ctx context.Context, h *ApiHandle, p *Policy) (*Policy, error) {
jsonP, err := json.Marshal(p)
if err != nil {
return nil, err
}
reqJson := fmt.Sprintf(r.config.SetMethod.RequestFormat, jsonP)
if !json.Valid([]byte(reqJson)) {
return nil, fmt.Errorf("request format from generated IAM config invalid JSON: %s", reqJson)
}
var policy Policy
if err := h.DoSetRequest(ctx, r, strings.NewReader(reqJson), &policy); err != nil {
return nil, errwrap.Wrapf("unable to set policy: {{err}}", err)
}
return &policy, nil
}