Skip to content

Commit bd0977c

Browse files
Add secret data source
1 parent e8da9e4 commit bd0977c

File tree

8 files changed

+267
-2
lines changed

8 files changed

+267
-2
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,9 @@ For running the acceptance tests you also have to set additionnal environment va
6060

6161
* `TF_VAR_test_project_id` to an already provisioned writable project
6262
* `TF_VAR_test_project_ids` to already provisioned writable projects (comma separated)
63-
* `TF_VAR_test_abx_action_id` to an already provisioned ABX action
6463
* `TF_VAR_test_icon_id` to an already provisioned Icon
64+
* `TF_VAR_test_secret_id` to an already provisioned Secret
65+
* `TF_VAR_test_abx_action_id` to an already provisioned ABX action
6566

6667
Resources generated by the acceptance tests will be generated "inside" given project.
6768

@@ -77,8 +78,9 @@ export ARIA_ACCESS_TOKEN=***** # If you have one, not required
7778

7879
export TF_VAR_test_project_id=2e34b115-dd18-48b3-a6af-f794469e5e0d
7980
export TF_VAR_test_project_ids=8f274902-94dc-40fd-98b5-f06c68ae1237,a9441e75-57c0-46fa-9262-c06a47acb1a9,2e34b115-dd18-48b3-a6af-f794469e5e0d
80-
export TF_VAR_test_abx_action_id=8a7480d38e535332018e857e0d4f3437
8181
export TF_VAR_test_icon_id=72a9a2c7-494e-31d7-afe8-cd27479c407e
82+
export TF_VAR_test_secret_id=a9af6450-a0c6-42cf-921e-14f7f8db50b3
83+
export TF_VAR_test_abx_action_id=8a7480d38e535332018e857e0d4f3437
8284

8385
make testacc
8486
```

docs/data-sources/secret.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "aria_secret Data Source - aria"
4+
subcategory: ""
5+
description: |-
6+
Secret data source
7+
---
8+
9+
# aria_secret (Data Source)
10+
11+
Secret data source
12+
13+
## Example Usage
14+
15+
```terraform
16+
data "aria_secret" "example" {
17+
id = "a9af6450-a0c6-42cf-921e-14f7f8db50b3"
18+
}
19+
```
20+
21+
<!-- schema generated by tfplugindocs -->
22+
## Schema
23+
24+
### Required
25+
26+
- `id` (String) Secret identifier
27+
28+
### Read-Only
29+
30+
- `created_at` (String) Creation date-time
31+
- `created_by` (String) Ask VMware
32+
- `description` (String) Describe the secret in few sentences
33+
- `name` (String) Secret name
34+
- `org_id` (String) Organisation ID
35+
- `org_scoped` (Boolean) Scoped to the organization?
36+
- `project_ids` (Set of String) Restrict to given projects (an empty list means all)
37+
- `updated_at` (String) Changed date-time
38+
- `updated_by` (String) Ask VMware
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
data "aria_secret" "example" {
2+
id = "a9af6450-a0c6-42cf-921e-14f7f8db50b3"
3+
}

examples/resources/aria_abx_action/resource.tf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ resource "aria_abx_action" "hello_world" {
3131

3232
project_id = var.test_project_id
3333

34+
shared = true
35+
3436
source = <<EOT
3537
from __future__ import annotations
3638

internal/provider/models.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,56 @@ type IconModel struct {
347347
Content types.String `tfsdk:"content"`
348348
}
349349

350+
// SecretModel describes the secret model.
351+
type SecretModel struct {
352+
Id types.String `tfsdk:"id"`
353+
Name types.String `tfsdk:"name"`
354+
// Value
355+
Description types.String `tfsdk:"description"`
356+
OrgId types.String `tfsdk:"org_id"`
357+
OrgScoped types.Bool `tfsdk:"org_scoped"`
358+
ProjectIds types.Set `tfsdk:"project_ids"`
359+
CreatedAt types.String `tfsdk:"created_at"`
360+
CreatedBy types.String `tfsdk:"created_by"`
361+
UpdatedAt types.String `tfsdk:"updated_at"`
362+
UpdatedBy types.String `tfsdk:"updated_by"`
363+
}
364+
365+
// SecretAPIModel describes the secret API model.
366+
type SecretAPIModel struct {
367+
Id string `json:"id"`
368+
Name string `json:"name"`
369+
// Value
370+
Description string `json:"description"`
371+
OrgId string `json:"orgId"`
372+
OrgScoped bool `json:"orgScoped"`
373+
ProjectIds []string `json:"projectIds"`
374+
CreatedAt string `json:"createdAt"`
375+
CreatedBy string `json:"createdBy"`
376+
UpdatedAt string `json:"updatedAt"`
377+
UpdatedBy string `json:"updatedBy"`
378+
}
379+
380+
func (self *SecretModel) FromAPI(
381+
ctx context.Context,
382+
raw SecretAPIModel,
383+
) diag.Diagnostics {
384+
projectIds, diags := types.SetValueFrom(ctx, types.StringType, raw.ProjectIds)
385+
386+
self.Id = types.StringValue(raw.Id)
387+
self.Name = types.StringValue(raw.Name)
388+
self.Description = types.StringValue(raw.Description)
389+
self.OrgId = types.StringValue(raw.OrgId)
390+
self.OrgScoped = types.BoolValue(raw.OrgScoped)
391+
self.ProjectIds = projectIds
392+
self.CreatedAt = types.StringValue(raw.CreatedAt)
393+
self.CreatedBy = types.StringValue(raw.CreatedBy)
394+
self.UpdatedAt = types.StringValue(raw.UpdatedAt)
395+
self.UpdatedBy = types.StringValue(raw.UpdatedBy)
396+
397+
return diags
398+
}
399+
350400
// SubscriptionModel describes the resource data model.
351401
type SubscriptionModel struct {
352402
Id types.String `tfsdk:"id"`

internal/provider/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ func (self *AriaProvider) DataSources(ctx context.Context) []func() datasource.D
234234
return []func() datasource.DataSource{
235235
NewCatalogTypeDataSource,
236236
NewIconDataSource,
237+
NewSecretDataSource,
237238
}
238239
}
239240

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
// Copyright (c) State of Geneva (Switzerland)
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package provider
5+
6+
import (
7+
"context"
8+
"fmt"
9+
10+
"github.com/go-resty/resty/v2"
11+
"github.com/hashicorp/terraform-plugin-framework/datasource"
12+
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
13+
"github.com/hashicorp/terraform-plugin-framework/types"
14+
)
15+
16+
// Ensure provider defined types fully satisfy framework interfaces.
17+
var _ datasource.DataSource = &SecretDataSource{}
18+
19+
func NewSecretDataSource() datasource.DataSource {
20+
return &SecretDataSource{}
21+
}
22+
23+
// SecretDataSource defines the data source implementation.
24+
type SecretDataSource struct {
25+
client *resty.Client
26+
}
27+
28+
func (self *SecretDataSource) Metadata(
29+
ctx context.Context,
30+
req datasource.MetadataRequest,
31+
resp *datasource.MetadataResponse,
32+
) {
33+
resp.TypeName = req.ProviderTypeName + "_secret"
34+
}
35+
36+
func (self *SecretDataSource) Schema(
37+
ctx context.Context,
38+
req datasource.SchemaRequest,
39+
resp *datasource.SchemaResponse,
40+
) {
41+
resp.Schema = schema.Schema{
42+
MarkdownDescription: "Secret data source",
43+
Attributes: map[string]schema.Attribute{
44+
"id": schema.StringAttribute{
45+
MarkdownDescription: "Secret identifier",
46+
Required: true,
47+
},
48+
"name": schema.StringAttribute{
49+
MarkdownDescription: "Secret name",
50+
Computed: true,
51+
},
52+
"description": schema.StringAttribute{
53+
MarkdownDescription: "Describe the secret in few sentences",
54+
Computed: true,
55+
},
56+
"org_id": schema.StringAttribute{
57+
MarkdownDescription: "Organisation ID",
58+
Computed: true,
59+
},
60+
"org_scoped": schema.BoolAttribute{
61+
MarkdownDescription: "Scoped to the organization?",
62+
Computed: true,
63+
},
64+
"project_ids": schema.SetAttribute{
65+
MarkdownDescription: "Restrict to given projects (an empty list means all)",
66+
ElementType: types.StringType,
67+
Computed: true,
68+
},
69+
"created_at": schema.StringAttribute{
70+
MarkdownDescription: "Creation date-time",
71+
Computed: true,
72+
},
73+
"created_by": schema.StringAttribute{
74+
MarkdownDescription: "Ask VMware",
75+
Computed: true,
76+
},
77+
"updated_at": schema.StringAttribute{
78+
MarkdownDescription: "Changed date-time",
79+
Computed: true,
80+
},
81+
"updated_by": schema.StringAttribute{
82+
MarkdownDescription: "Ask VMware",
83+
Computed: true,
84+
},
85+
},
86+
}
87+
}
88+
89+
func (self *SecretDataSource) Configure(
90+
ctx context.Context,
91+
req datasource.ConfigureRequest,
92+
resp *datasource.ConfigureResponse,
93+
) {
94+
self.client = GetDataSourceClient(ctx, req, resp)
95+
}
96+
97+
func (self *SecretDataSource) Read(
98+
ctx context.Context,
99+
req datasource.ReadRequest,
100+
resp *datasource.ReadResponse,
101+
) {
102+
// Read Terraform configuration data into the model
103+
var secret SecretModel
104+
resp.Diagnostics.Append(req.Config.Get(ctx, &secret)...)
105+
if resp.Diagnostics.HasError() {
106+
return
107+
}
108+
109+
var secretRaw SecretAPIModel
110+
secretId := secret.Id.ValueString()
111+
response, err := self.client.R().
112+
SetResult(&secretRaw).
113+
Get("/platform/api/secrets/" + secretId)
114+
115+
err = handleAPIResponse(ctx, response, err, 200)
116+
if err != nil {
117+
resp.Diagnostics.AddError(
118+
"Client error",
119+
fmt.Sprintf("Unable to read secret %s, got error: %s", secretId, err))
120+
return
121+
}
122+
123+
// Save updated secret into Terraform state
124+
resp.Diagnostics.Append(secret.FromAPI(ctx, secretRaw)...)
125+
resp.Diagnostics.Append(resp.State.Set(ctx, &secret)...)
126+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright (c) State of Geneva (Switzerland)
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package provider
5+
6+
import (
7+
"testing"
8+
9+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
10+
)
11+
12+
func TestAccSecretDataSource(t *testing.T) {
13+
resource.Test(t, resource.TestCase{
14+
PreCheck: func() { testAccPreCheck(t) },
15+
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
16+
Steps: []resource.TestStep{
17+
// Read testing
18+
{
19+
Config: `
20+
variable "test_secret_id" {
21+
description = "Secret to use for testing the data source."
22+
type = string
23+
}
24+
25+
data "aria_secret" "secret" {
26+
id = var.test_secret_id
27+
}`,
28+
Check: resource.ComposeAggregateTestCheckFunc(
29+
resource.TestCheckResourceAttrSet("data.aria_secret.secret", "id"),
30+
resource.TestCheckResourceAttrSet("data.aria_secret.secret", "name"),
31+
resource.TestCheckResourceAttrSet("data.aria_secret.secret", "description"),
32+
resource.TestCheckResourceAttrSet("data.aria_secret.secret", "org_id"),
33+
resource.TestCheckResourceAttrSet("data.aria_secret.secret", "org_scoped"),
34+
// resource.TestCheckResourceAttrSet("data.aria_secret.secret", "project_ids"),
35+
resource.TestCheckResourceAttrSet("data.aria_secret.secret", "created_at"),
36+
resource.TestCheckResourceAttrSet("data.aria_secret.secret", "created_by"),
37+
resource.TestCheckResourceAttrSet("data.aria_secret.secret", "updated_at"),
38+
resource.TestCheckResourceAttrSet("data.aria_secret.secret", "updated_by"),
39+
),
40+
},
41+
},
42+
})
43+
}

0 commit comments

Comments
 (0)