Skip to content

Commit 33190b5

Browse files
INTMDB-137: Add Get Role to CloudProviderAccessService (#498)
1 parent 57ec4a2 commit 33190b5

File tree

2 files changed

+79
-6
lines changed

2 files changed

+79
-6
lines changed

mongodbatlas/cloud_provider_access.go

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@ const cloudProviderAccessPath = "api/atlas/v1.0/groups/%s/cloudProviderAccess"
2424

2525
// CloudProviderAccessService provides access to the cloud provider access functions in the Atlas API.
2626
//
27-
// See more: https://docs.atlas.mongodb.com/reference/api/cloud-provider-access/
27+
// See more: https://www.mongodb.com/docs/atlas/reference/api-resources-spec/v2/#tag/Cloud-Provider-Access
2828
type CloudProviderAccessService interface {
2929
ListRoles(context.Context, string) (*CloudProviderAccessRoles, *Response, error)
30+
GetRole(context.Context, string, string) (*CloudProviderAccessRoles, *Response, error)
3031
CreateRole(context.Context, string, *CloudProviderAccessRoleRequest) (*AWSIAMRole, *Response, error)
3132
AuthorizeRole(context.Context, string, string, *CloudProviderAuthorizationRequest) (*AWSIAMRole, *Response, error)
3233
DeauthorizeRole(context.Context, *CloudProviderDeauthorizationRequest) (*Response, error)
@@ -78,9 +79,37 @@ type CloudProviderDeauthorizationRequest struct {
7879
RoleID string
7980
}
8081

81-
// ListRoles retrieve existing AWS IAM roles.
82+
// GetRole Returns the Amazon Web Services (AWS) Identity and Access Management (IAM) role
83+
// with the specified id and with access to the specified project.
8284
//
83-
// See more: https://docs.atlas.mongodb.com/reference/api/cloud-provider-access-get-roles/
85+
// See more: https://www.mongodb.com/docs/atlas/reference/api-resources-spec/v2/#tag/Cloud-Provider-Access/operation/getCloudProviderAccessRole
86+
func (s *CloudProviderAccessServiceOp) GetRole(ctx context.Context, groupID, roleID string) (*CloudProviderAccessRoles, *Response, error) {
87+
if groupID == "" {
88+
return nil, nil, NewArgError("groupId", "must be set")
89+
}
90+
if roleID == "" {
91+
return nil, nil, NewArgError("roleID", "must be set")
92+
}
93+
94+
basePath := fmt.Sprintf(cloudProviderAccessPath, groupID)
95+
path := fmt.Sprintf("%s/%s", basePath, roleID)
96+
req, err := s.Client.NewRequest(ctx, http.MethodGet, path, nil)
97+
if err != nil {
98+
return nil, nil, err
99+
}
100+
101+
root := new(CloudProviderAccessRoles)
102+
resp, err := s.Client.Do(ctx, req, root)
103+
if err != nil {
104+
return nil, resp, err
105+
}
106+
107+
return root, resp, nil
108+
}
109+
110+
// ListRoles retrieves existing AWS IAM roles.
111+
//
112+
// See more: https://www.mongodb.com/docs/atlas/reference/api-resources-spec/v2/#tag/Cloud-Provider-Access/operation/listCloudProviderAccessRoles
84113
func (s *CloudProviderAccessServiceOp) ListRoles(ctx context.Context, groupID string) (*CloudProviderAccessRoles, *Response, error) {
85114
path := fmt.Sprintf(cloudProviderAccessPath, groupID)
86115

@@ -100,7 +129,7 @@ func (s *CloudProviderAccessServiceOp) ListRoles(ctx context.Context, groupID st
100129

101130
// CreateRole creates an AWS IAM role.
102131
//
103-
// See more: https://docs.atlas.mongodb.com/reference/api/cloud-provider-access-create-one-role/
132+
// See more: https://www.mongodb.com/docs/atlas/reference/api-resources-spec/v2/#tag/Cloud-Provider-Access/operation/createCloudProviderAccessRole
104133
func (s *CloudProviderAccessServiceOp) CreateRole(ctx context.Context, groupID string, request *CloudProviderAccessRoleRequest) (*AWSIAMRole, *Response, error) {
105134
if request == nil {
106135
return nil, nil, NewArgError("request", "must be set")
@@ -124,7 +153,7 @@ func (s *CloudProviderAccessServiceOp) CreateRole(ctx context.Context, groupID s
124153

125154
// AuthorizeRole authorizes and configure an AWS Assumed IAM role.
126155
//
127-
// See more: https://docs.atlas.mongodb.com/reference/api/cloud-provider-access-authorize-one-role/
156+
// See more: https://www.mongodb.com/docs/atlas/reference/api-resources-spec/v2/#tag/Cloud-Provider-Access/operation/authorizeCloudProviderAccessRole
128157
func (s *CloudProviderAccessServiceOp) AuthorizeRole(ctx context.Context, groupID, roleID string, request *CloudProviderAuthorizationRequest) (*AWSIAMRole, *Response, error) {
129158
if roleID == "" {
130159
return nil, nil, NewArgError("roleID", "must be set")
@@ -153,7 +182,7 @@ func (s *CloudProviderAccessServiceOp) AuthorizeRole(ctx context.Context, groupI
153182

154183
// DeauthorizeRole deauthorizes an AWS Assumed IAM role.
155184
//
156-
// See more: https://docs.atlas.mongodb.com/reference/api/cloud-provider-access-deauthorize-one-role/
185+
// See more: https://www.mongodb.com/docs/atlas/reference/api-resources-spec/v2/#tag/Cloud-Provider-Access/operation/deauthorizeCloudProviderAccessRole
157186
func (s *CloudProviderAccessServiceOp) DeauthorizeRole(ctx context.Context, request *CloudProviderDeauthorizationRequest) (*Response, error) {
158187
if request.RoleID == "" {
159188
return nil, NewArgError("roleID", "must be set")

mongodbatlas/cloud_provider_access_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,50 @@ func TestCloudProviderAccessServiceOp_ListRoles(t *testing.T) {
6767
}
6868
}
6969

70+
func TestCloudProviderAccessServiceOp_GetRole(t *testing.T) {
71+
client, mux, teardown := setup()
72+
defer teardown()
73+
roleID := "1"
74+
mux.HandleFunc(fmt.Sprintf("/api/atlas/v1.0/groups/1/cloudProviderAccess/%s", roleID), func(w http.ResponseWriter, r *http.Request) {
75+
testMethod(t, r, http.MethodGet)
76+
fmt.Fprint(w, `{
77+
"awsIamRoles": [{
78+
"atlasAWSAccountArn": "arn:aws:iam::123456789012:root",
79+
"atlasAssumedRoleExternalId": "3192be49-6e76-4b7d-a7b8-b486a8fc4483",
80+
"authorizedDate": "2020-08-03T20:42:49Z",
81+
"createdDate": "2020-07-30T20:20:36Z",
82+
"featureUsages": [],
83+
"iamAssumedRoleArn": "arn:aws:iam::772401394250:role/my-test-aws-role",
84+
"providerName": "AWS",
85+
"roleId": "5f232b94af0a6b41747bcc2d"
86+
}]
87+
}`)
88+
})
89+
90+
roles, _, err := client.CloudProviderAccess.GetRole(ctx, groupID, roleID)
91+
if err != nil {
92+
t.Fatalf("CloudProviderAccess.GetRole returned error: %v", err)
93+
}
94+
95+
expected := &CloudProviderAccessRoles{
96+
AWSIAMRoles: []AWSIAMRole{
97+
{
98+
AtlasAWSAccountARN: "arn:aws:iam::123456789012:root",
99+
AtlasAssumedRoleExternalID: "3192be49-6e76-4b7d-a7b8-b486a8fc4483",
100+
AuthorizedDate: "2020-08-03T20:42:49Z",
101+
CreatedDate: "2020-07-30T20:20:36Z",
102+
FeatureUsages: []*FeatureUsage{},
103+
IAMAssumedRoleARN: "arn:aws:iam::772401394250:role/my-test-aws-role",
104+
ProviderName: "AWS",
105+
RoleID: "5f232b94af0a6b41747bcc2d",
106+
},
107+
},
108+
}
109+
if diff := deep.Equal(roles, expected); diff != nil {
110+
t.Error(diff)
111+
}
112+
}
113+
70114
func TestCloudProviderAccessServiceOp_CreateRole(t *testing.T) {
71115
client, mux, teardown := setup()
72116
defer teardown()

0 commit comments

Comments
 (0)