Skip to content

Commit d1dd177

Browse files
committed
feat: add stacklet_platform data source
### what add a datasource to retrieve general details about the platform ### why provide general info about the deployment ### testing acceptance tests and tested in sandbox ### docs added here
1 parent b320ab9 commit d1dd177

File tree

11 files changed

+253
-2
lines changed

11 files changed

+253
-2
lines changed

docs/data-sources/platform.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "stacklet_platform Data Source - terraform-provider-stacklet"
4+
subcategory: ""
5+
description: |-
6+
Retrieve information about the Stacklet platform.
7+
---
8+
9+
# stacklet_platform (Data Source)
10+
11+
Retrieve information about the Stacklet platform.
12+
13+
## Example Usage
14+
15+
```terraform
16+
# Fetch details about the platform
17+
data "stacklet_platform" "example" {}
18+
```
19+
20+
<!-- schema generated by tfplugindocs -->
21+
## Schema
22+
23+
### Read-Only
24+
25+
- `default_role` (String) Default role for users.
26+
- `execution_regions` (List of String) List of regions for which execution is enabled.
27+
- `external_id` (String) The external ID for the deployment.
28+
- `id` (String) The GraphQL Node ID.

docs/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ It allows managing resources like accounts, account groups, policy collections,
1818
terraform {
1919
required_providers {
2020
stacklet = {
21-
source = "registry.terraform.io/stacklet/stacklet"
21+
source = "stacklet/stacklet"
2222
}
2323
}
2424
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Fetch details about the platform
2+
data "stacklet_platform" "example" {}

examples/provider/provider.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
terraform {
22
required_providers {
33
stacklet = {
4-
source = "registry.terraform.io/stacklet/stacklet"
4+
source = "stacklet/stacklet"
55
}
66
}
77
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright (c) 2025 - Stacklet, Inc.
2+
3+
package acceptance_tests
4+
5+
import (
6+
"testing"
7+
8+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
9+
)
10+
11+
func TestAccPlatformDataSource(t *testing.T) {
12+
steps := []resource.TestStep{
13+
{
14+
Config: `
15+
data "stacklet_platform" "test" {}
16+
`,
17+
Check: resource.ComposeAggregateTestCheckFunc(
18+
resource.TestCheckResourceAttrSet("data.stacklet_platform.test", "id"),
19+
resource.TestCheckResourceAttrSet("data.stacklet_platform.test", "external_id"),
20+
// at least one region is enabled
21+
resource.TestCheckResourceAttrSet("data.stacklet_platform.test", "execution_regions.0"),
22+
resource.TestCheckResourceAttrSet("data.stacklet_platform.test", "default_role"),
23+
),
24+
},
25+
}
26+
runRecordedAccTest(t, "TestAccPlatformDataSource", steps)
27+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
{
2+
"{platform{id,externalID,executionRegions,defaultRole}}": [
3+
{
4+
"request": {
5+
"query": "{platform{id,externalID,executionRegions,defaultRole}}"
6+
},
7+
"response": {
8+
"data": {
9+
"platform": {
10+
"defaultRole": "admin",
11+
"executionRegions": [
12+
"eu-north-1",
13+
"us-east-1"
14+
],
15+
"externalID": "external=1e28+b6b6/cf0a-4f54.b604,9218@4cf2:50d2",
16+
"id": "WyJwbGF0Zm9ybSJd"
17+
}
18+
}
19+
}
20+
},
21+
{
22+
"request": {
23+
"query": "{platform{id,externalID,executionRegions,defaultRole}}"
24+
},
25+
"response": {
26+
"data": {
27+
"platform": {
28+
"defaultRole": "admin",
29+
"executionRegions": [
30+
"eu-north-1",
31+
"us-east-1"
32+
],
33+
"externalID": "external=1e28+b6b6/cf0a-4f54.b604,9218@4cf2:50d2",
34+
"id": "WyJwbGF0Zm9ybSJd"
35+
}
36+
}
37+
}
38+
},
39+
{
40+
"request": {
41+
"query": "{platform{id,externalID,executionRegions,defaultRole}}"
42+
},
43+
"response": {
44+
"data": {
45+
"platform": {
46+
"defaultRole": "admin",
47+
"executionRegions": [
48+
"eu-north-1",
49+
"us-east-1"
50+
],
51+
"externalID": "external=1e28+b6b6/cf0a-4f54.b604,9218@4cf2:50d2",
52+
"id": "WyJwbGF0Zm9ybSJd"
53+
}
54+
}
55+
}
56+
}
57+
]
58+
}

internal/api/api.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ type API struct {
1414
AccountGroup accountGroupAPI
1515
AccountGroupMapping accountGroupMappingAPI
1616
Binding bindingAPI
17+
Platform platformAPI
1718
Policy policyAPI
1819
PolicyCollection policyCollectionAPI
1920
PolicyCollectionMapping policyCollectionMappingAPI
@@ -28,6 +29,7 @@ func New(c *graphql.Client) *API {
2829
AccountGroup: accountGroupAPI{c},
2930
AccountGroupMapping: accountGroupMappingAPI{c},
3031
Binding: bindingAPI{c},
32+
Platform: platformAPI{c},
3133
Policy: policyAPI{c},
3234
PolicyCollection: policyCollectionAPI{c},
3335
PolicyCollectionMapping: policyCollectionMappingAPI{c},

internal/api/platform.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright (c) 2025 - Stacklet, Inc.
2+
3+
package api
4+
5+
import (
6+
"context"
7+
8+
"github.com/hasura/go-graphql-client"
9+
)
10+
11+
// Platform is the data returned by reading platform data.
12+
type Platform struct {
13+
ID string
14+
ExternalID *string `graphql:"externalID"`
15+
ExecutionRegions []string
16+
DefaultRole *string
17+
}
18+
19+
type platformAPI struct {
20+
c *graphql.Client
21+
}
22+
23+
// Read returns platform data.
24+
func (a platformAPI) Read(ctx context.Context) (*Platform, error) {
25+
var query struct {
26+
Platform Platform `graphql:"platform"`
27+
}
28+
if err := a.c.Query(ctx, &query, nil); err != nil {
29+
return nil, NewAPIError(err)
30+
}
31+
return &query.Platform, nil
32+
}

internal/datasources/datasources.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ var DATASOURCES = []func() datasource.DataSource{
1010
NewAccountDataSource,
1111
NewAccountGroupDataSource,
1212
NewBindingDataSource,
13+
NewPlatformDataSource,
1314
NewPolicyCollectionDataSource,
1415
NewPolicyDataSource,
1516
NewRepositoryDataSource,

internal/datasources/platform.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// Copyright (c) 2025 - Stacklet, Inc.
2+
3+
package datasources
4+
5+
import (
6+
"context"
7+
8+
"github.com/hashicorp/terraform-plugin-framework/datasource"
9+
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
10+
"github.com/hashicorp/terraform-plugin-framework/types"
11+
12+
"github.com/stacklet/terraform-provider-stacklet/internal/api"
13+
"github.com/stacklet/terraform-provider-stacklet/internal/errors"
14+
"github.com/stacklet/terraform-provider-stacklet/internal/models"
15+
"github.com/stacklet/terraform-provider-stacklet/internal/providerdata"
16+
tftypes "github.com/stacklet/terraform-provider-stacklet/internal/types"
17+
)
18+
19+
var (
20+
_ datasource.DataSource = &platformDataSource{}
21+
)
22+
23+
func NewPlatformDataSource() datasource.DataSource {
24+
return &platformDataSource{}
25+
}
26+
27+
type platformDataSource struct {
28+
api *api.API
29+
}
30+
31+
func (d *platformDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
32+
resp.TypeName = req.ProviderTypeName + "_platform"
33+
}
34+
35+
func (d *platformDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
36+
resp.Schema = schema.Schema{
37+
Description: "Retrieve information about the Stacklet platform.",
38+
Attributes: map[string]schema.Attribute{
39+
"id": schema.StringAttribute{
40+
Description: "The GraphQL Node ID.",
41+
Computed: true,
42+
},
43+
"external_id": schema.StringAttribute{
44+
Description: "The external ID for the deployment.",
45+
Computed: true,
46+
},
47+
"execution_regions": schema.ListAttribute{
48+
Description: "List of regions for which execution is enabled.",
49+
Computed: true,
50+
ElementType: types.StringType,
51+
},
52+
"default_role": schema.StringAttribute{
53+
Description: "Default role for users.",
54+
Computed: true,
55+
},
56+
},
57+
}
58+
}
59+
60+
func (d *platformDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
61+
if pd, err := providerdata.GetDataSourceProviderData(req); err != nil {
62+
errors.AddDiagError(&resp.Diagnostics, err)
63+
} else if pd != nil {
64+
d.api = pd.API
65+
}
66+
}
67+
68+
func (d *platformDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
69+
var data models.PlatformDataSource
70+
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
71+
if resp.Diagnostics.HasError() {
72+
return
73+
}
74+
75+
platform, err := d.api.Platform.Read(ctx)
76+
if err != nil {
77+
errors.AddDiagError(&resp.Diagnostics, err)
78+
return
79+
}
80+
81+
data.ID = types.StringValue(platform.ID)
82+
data.ExternalID = tftypes.NullableString(platform.ExternalID)
83+
data.ExecutionRegions = tftypes.StringsList(platform.ExecutionRegions)
84+
data.DefaultRole = tftypes.NullableString(platform.DefaultRole)
85+
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
86+
}

0 commit comments

Comments
 (0)