Skip to content

Commit a03749e

Browse files
authored
Merge pull request #70 from arangodb-managed/OAS-5164
Oas 5164 | Add IAM Policy resource to Terraform provider
2 parents e2217cb + 4d3b856 commit a03749e

File tree

12 files changed

+726
-1
lines changed

12 files changed

+726
-1
lines changed

docs/data-sources/current_user.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "oasis_current_user Data Source - terraform-provider-oasis"
4+
subcategory: ""
5+
description: |-
6+
Oasis Current User Data Source
7+
---
8+
9+
# oasis_current_user (Data Source)
10+
11+
Oasis Current User Data Source
12+
13+
## Example Usage
14+
15+
```terraform
16+
terraform {
17+
required_version = ">= 0.13.0"
18+
required_providers {
19+
oasis = {
20+
source = "arangodb-managed/oasis"
21+
version = ">=2.1.2"
22+
}
23+
}
24+
}
25+
26+
provider "oasis" {
27+
api_key_id = "" // API Key ID generated in Oasis platform
28+
api_key_secret = "" // API Key Secret generated in Oasis platform
29+
}
30+
31+
// Load in an Oasis Current User within an organization
32+
data "oasis_current_user" "oasis_test_current_user" {}
33+
34+
// Output the data after it has been synced.
35+
output "datasets" {
36+
value = data.oasis_current_user.oasis_test_current_user
37+
}
38+
```
39+
40+
<!-- schema generated by tfplugindocs -->
41+
## Schema
42+
43+
### Optional
44+
45+
- `email` (String) Current User Data Source Email field
46+
- `id` (String) Current User Data Source User ID field
47+
- `name` (String) Current User Data Source Name field
48+
49+

docs/resources/iam_policy.md

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "oasis_iam_policy Resource - terraform-provider-oasis"
4+
subcategory: ""
5+
description: |-
6+
Oasis IAM Policy Resource
7+
---
8+
9+
# oasis_iam_policy (Resource)
10+
11+
Oasis IAM Policy Resource
12+
13+
## Example Usage
14+
15+
```terraform
16+
terraform {
17+
required_version = ">= 0.13.0"
18+
required_providers {
19+
oasis = {
20+
source = "arangodb-managed/oasis"
21+
version = ">=2.1.2"
22+
}
23+
}
24+
}
25+
26+
provider "oasis" {
27+
api_key_id = "" // API Key ID generated in Oasis platform
28+
api_key_secret = "" // API Key Secret generated in Oasis platform
29+
}
30+
31+
// Terraform created organization
32+
resource "oasis_organization" "oasis_test_organization" {
33+
name = "Terraform Oasis Organization"
34+
description = "A test Oasis organization from Terraform Provider"
35+
}
36+
37+
// Terraform created IAM Group. This resource uses the computed ID value of the
38+
// previously defined organization resource.
39+
resource "oasis_iam_group" "my_iam_group" {
40+
name = "Terraform IAM Group"
41+
description = "IAM Group created by Terraform"
42+
organization = oasis_organization.oasis_test_organization.id
43+
}
44+
45+
// Load in an Oasis Current User within an organization
46+
data "oasis_current_user" "oasis_test_current_user" {}
47+
48+
// Terraform created IAM Policy. This resource uses the computed ID value of the
49+
// previously defined organization resource and IAM group resource.
50+
resource "oasis_iam_policy" "my_iam_policy_group" {
51+
url = "/Organization/${oasis_organization.oasis_test_organization.id}"
52+
53+
binding {
54+
role = "auditlog-admin"
55+
group = oasis_iam_group.my_iam_group.id
56+
}
57+
58+
binding {
59+
role = "auditlog-archive-viewer"
60+
group = oasis_iam_group.my_iam_group.id
61+
}
62+
63+
binding {
64+
role = "auditlog-archive-viewer"
65+
user = data.oasis_current_user.oasis_test_current_user.id
66+
}
67+
}
68+
```
69+
70+
<!-- schema generated by tfplugindocs -->
71+
## Schema
72+
73+
### Required
74+
75+
- `binding` (Block List, Min: 1) IAM Policy Resource IAM Policy Bindings (see [below for nested schema](#nestedblock--binding))
76+
- `url` (String) IAM Policy Resource IAM Policy URL
77+
78+
### Read-Only
79+
80+
- `id` (String) The ID of this resource.
81+
82+
<a id="nestedblock--binding"></a>
83+
### Nested Schema for `binding`
84+
85+
Required:
86+
87+
- `role` (String) IAM Policy Resource IAM Policy Role
88+
89+
Optional:
90+
91+
- `group` (String) IAM Policy Resource IAM Policy Group
92+
- `user` (String) IAM Policy Resource IAM Policy User
93+
94+

docs/resources/private_endpoint.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ resource "oasis_private_endpoint" "my_aws_private_endpoint" {
134134
<a id="nestedblock--aks"></a>
135135
### Nested Schema for `aks`
136136

137-
Optional:
137+
Required:
138138

139139
- `az_client_subscription_ids` (List of String) Private Endpoint Resource Private Endpoint AKS Subscription IDS field (list of subscription ids)
140140

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Example: Current User Data Source
2+
3+
This example shows how to use the Terraform Oasis provider to manage Current User Data Source in Oasis.
4+
5+
## Prerequisites
6+
7+
*This example uses syntax elements specific to Terraform version 0.13+ (tested on Terraform version 1.1.4).
8+
It will not work out-of-the-box with Terraform 0.12.x and lower (deprecated by Terraform).*
9+
10+
## Environment variables
11+
Please refer to [Main README](../../README.md) file for all the environment variables you might need.
12+
13+
## Example output
14+
```
15+
datasets = {
16+
"email" = "[email protected]"
17+
"id" = "google-oauth2|781471512049238536110"
18+
"name" = "Test Arango"
19+
}
20+
```
21+
22+
## Instructions on how to run:
23+
```
24+
terraform init
25+
terraform plan
26+
terraform apply
27+
```
28+
29+
To remove the resources created run:
30+
```
31+
terraform destroy
32+
```
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
terraform {
2+
required_version = ">= 0.13.0"
3+
required_providers {
4+
oasis = {
5+
source = "arangodb-managed/oasis"
6+
version = ">=2.1.2"
7+
}
8+
}
9+
}
10+
11+
provider "oasis" {
12+
api_key_id = "" // API Key ID generated in Oasis platform
13+
api_key_secret = "" // API Key Secret generated in Oasis platform
14+
}
15+
16+
// Load in an Oasis Current User within an organization
17+
data "oasis_current_user" "oasis_test_current_user" {}
18+
19+
// Output the data after it has been synced.
20+
output "datasets" {
21+
value = data.oasis_current_user.oasis_test_current_user
22+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Example: IAM Policy
2+
3+
This example shows how to use the Terraform Oasis provider to create an IAM Policy for a specific resource within Oasis.
4+
5+
## Prerequisites
6+
7+
*This example uses syntax elements specific to Terraform version 0.13+ (tested on Terraform version 1.1.4).
8+
It will not work out-of-the-box with Terraform 0.12.x and lower (deprecated by Terraform).*
9+
10+
## Environment variables
11+
Please refer to [Main README](../../README.md) file for all the environment variables you might need.
12+
13+
## Instructions on how to run:
14+
```
15+
terraform init
16+
terraform plan
17+
terraform apply
18+
```
19+
20+
To remove the resources created run:
21+
```
22+
terraform destroy
23+
```
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
terraform {
2+
required_version = ">= 0.13.0"
3+
required_providers {
4+
oasis = {
5+
source = "arangodb-managed/oasis"
6+
version = ">=2.1.2"
7+
}
8+
}
9+
}
10+
11+
provider "oasis" {
12+
api_key_id = "" // API Key ID generated in Oasis platform
13+
api_key_secret = "" // API Key Secret generated in Oasis platform
14+
}
15+
16+
// Terraform created organization
17+
resource "oasis_organization" "oasis_test_organization" {
18+
name = "Terraform Oasis Organization"
19+
description = "A test Oasis organization from Terraform Provider"
20+
}
21+
22+
// Terraform created IAM Group. This resource uses the computed ID value of the
23+
// previously defined organization resource.
24+
resource "oasis_iam_group" "my_iam_group" {
25+
name = "Terraform IAM Group"
26+
description = "IAM Group created by Terraform"
27+
organization = oasis_organization.oasis_test_organization.id
28+
}
29+
30+
// Load in an Oasis Current User within an organization
31+
data "oasis_current_user" "oasis_test_current_user" {}
32+
33+
// Terraform created IAM Policy. This resource uses the computed ID value of the
34+
// previously defined organization resource and IAM group resource.
35+
resource "oasis_iam_policy" "my_iam_policy_group" {
36+
url = "/Organization/${oasis_organization.oasis_test_organization.id}"
37+
38+
binding {
39+
role = "auditlog-admin"
40+
group = oasis_iam_group.my_iam_group.id
41+
}
42+
43+
binding {
44+
role = "auditlog-archive-viewer"
45+
group = oasis_iam_group.my_iam_group.id
46+
}
47+
48+
binding {
49+
role = "auditlog-archive-viewer"
50+
user = data.oasis_current_user.oasis_test_current_user.id
51+
}
52+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
//
2+
// DISCLAIMER
3+
//
4+
// Copyright 2022 ArangoDB GmbH, Cologne, Germany
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+
// Copyright holder is ArangoDB GmbH, Cologne, Germany
19+
//
20+
21+
package provider
22+
23+
import (
24+
"context"
25+
26+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
27+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
28+
29+
common "github.com/arangodb-managed/apis/common/v1"
30+
iam "github.com/arangodb-managed/apis/iam/v1"
31+
)
32+
33+
const (
34+
// current user source fields
35+
userIdFieldName = "id"
36+
userEmailFieldName = "email"
37+
userNameFieldName = "name"
38+
)
39+
40+
// dataSourceOasisCurrentUser defines a Current User datasource terraform type.
41+
func dataSourceOasisCurrentUser() *schema.Resource {
42+
return &schema.Resource{
43+
Description: "Oasis Current User Data Source",
44+
ReadContext: dataSourceOasisCurrentUserRead,
45+
46+
Schema: map[string]*schema.Schema{
47+
userIdFieldName: {
48+
Type: schema.TypeString,
49+
Description: "Current User Data Source User ID field",
50+
Optional: true,
51+
},
52+
userEmailFieldName: {
53+
Type: schema.TypeString,
54+
Description: "Current User Data Source Email field",
55+
Optional: true,
56+
},
57+
userNameFieldName: {
58+
Type: schema.TypeString,
59+
Description: "Current User Data Source Name field",
60+
Optional: true,
61+
},
62+
},
63+
}
64+
}
65+
66+
// dataSourceOasisCurrentUserRead reloads the resource object from the terraform store.
67+
func dataSourceOasisCurrentUserRead(ctx context.Context, data *schema.ResourceData, m interface{}) diag.Diagnostics {
68+
client := m.(*Client)
69+
if err := client.Connect(); err != nil {
70+
client.log.Error().Err(err).Msg("Failed to connect to api")
71+
return diag.FromErr(err)
72+
}
73+
74+
iamc := iam.NewIAMServiceClient(client.conn)
75+
user, err := iamc.GetThisUser(client.ctxWithToken, &common.Empty{})
76+
if err != nil {
77+
return diag.FromErr(err)
78+
}
79+
80+
for k, v := range flattenCurrentUserObject(user) {
81+
if err := data.Set(k, v); err != nil {
82+
return diag.FromErr(err)
83+
}
84+
}
85+
data.SetId(user.GetId())
86+
return nil
87+
}
88+
89+
// flattenCurrentUserObject creates a map from an Oasis Current User for easy digestion by the terraform schema.
90+
func flattenCurrentUserObject(user *iam.User) map[string]interface{} {
91+
return map[string]interface{}{
92+
userIdFieldName: user.GetId(),
93+
userEmailFieldName: user.GetEmail(),
94+
userNameFieldName: user.GetName(),
95+
}
96+
}

0 commit comments

Comments
 (0)