Skip to content

Commit 4278457

Browse files
authored
Implement polaris_aws_exocompute_cluster_attachment read operation (#196)
1 parent a548eaf commit 4278457

6 files changed

+80
-18
lines changed

docs/resources/aws_exocompute_cluster_attachment.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,12 @@ resource "polaris_aws_exocompute_cluster_attachment" "attachment" {
3333

3434
### Optional
3535

36-
- `token_refresh` (Number) To force a refresh of the token, part of the connection command, increase the value of this field. The token is valid for 24 hours.
36+
- `token_refresh` (Number) To force a refresh of the authentication token, part of the connection command and manifest, increase the value of this field. The token is valid for 24 hours.
3737

3838
### Read-Only
3939

40-
- `connection_command` (String) `kubectl` command which can be executed inside the EKS cluster to create a connection between the cluster and RSC. See setup_yaml for an alternative connection method.
40+
- `connection_command` (String) Kubernetes `kubectl` command used to create a connection between the cluster and RSC. See `manifest` for an alternative connection method.
41+
- `connection_command_executed` (Boolean) Whether the connection command has been executed or the manifest has been applied.
4142
- `id` (String) RSC cluster ID (UUID).
42-
- `setup_yaml` (String) K8s spec which can be passed to `kubectl apply` inside the EKS cluster to create a connection between the cluster and RSC. See connection_command for an alternative connection method.
43+
- `manifest` (String) Kubernetes manifest which can be passed to `kubectl apply` to create a connection between the cluster and RSC. See `connection_command` for an alternative connection method.
44+
- `setup_yaml` (String, Deprecated) Kubernetes manifest which can be passed to `kubectl apply` to create a connection between the cluster and RSC. See `connection_command` for an alternative connection method. **Deprecated:** use `manifest` instead.

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ require (
88
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320
99
github.com/hashicorp/terraform-plugin-docs v0.16.0
1010
github.com/hashicorp/terraform-plugin-sdk/v2 v2.10.0
11-
github.com/rubrikinc/rubrik-polaris-sdk-for-go v0.11.0-beta.2
11+
github.com/rubrikinc/rubrik-polaris-sdk-for-go v0.11.0-beta.3
1212
)
1313

1414
require (

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -412,8 +412,8 @@ github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6L
412412
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
413413
github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBOAvL+k=
414414
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
415-
github.com/rubrikinc/rubrik-polaris-sdk-for-go v0.11.0-beta.2 h1:JQlwbV6KsgEJ9CczJAWYvKS5cCc9ozuB0wV7lKQBrDI=
416-
github.com/rubrikinc/rubrik-polaris-sdk-for-go v0.11.0-beta.2/go.mod h1:ryJGDKlbaCvozY3Wvt+TPSN2OZRChQedHUNsnVfCbXE=
415+
github.com/rubrikinc/rubrik-polaris-sdk-for-go v0.11.0-beta.3 h1:+MG7xsQXCJOPVkhslAy2kkofkqZAqNME1r/bBaQrHbY=
416+
github.com/rubrikinc/rubrik-polaris-sdk-for-go v0.11.0-beta.3/go.mod h1:ryJGDKlbaCvozY3Wvt+TPSN2OZRChQedHUNsnVfCbXE=
417417
github.com/russross/blackfriday v1.6.0 h1:KqfZb0pUVN2lYqZUYRddxF4OR8ZMURnJIG5Y3VRLtww=
418418
github.com/russross/blackfriday v1.6.0/go.mod h1:ti0ldHuxg49ri4ksnFxlkCfN+hvslNlmVHqNRXXJNAY=
419419
github.com/sebdah/goldie v1.0.0/go.mod h1:jXP4hmWywNEwZzhMuv2ccnqTSFpuq8iyQhtQdkkZBH4=

internal/provider/names.go

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const (
2020
keyClusterName = "cluster_name"
2121
keyClusterSecurityGroupID = "cluster_security_group_id"
2222
keyConnectionCommand = "connection_command"
23+
keyConnectionCommandExecuted = "connection_command_executed"
2324
keyConnectionStatus = "connection_status"
2425
keyContainerName = "container_name"
2526
keyCustomerManagedKey = "customer_managed_key"
@@ -49,6 +50,7 @@ const (
4950
keyKMSMasterKey = "kms_master_key"
5051
keyLocationTemplate = "location_template"
5152
keyManagedPolicies = "managed_policies"
53+
keyManifest = "manifest"
5254
keyName = "name"
5355
keyNativeID = "native_id"
5456
keyNodeSecurityGroupID = "node_security_group_id"

internal/provider/resource_aws_exocompute_cluster_attachment.go

+60-10
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,13 @@ func resourceAwsExocomputeClusterAttachment() *schema.Resource {
6161
keyConnectionCommand: {
6262
Type: schema.TypeString,
6363
Computed: true,
64-
Description: "`kubectl` command which can be executed inside the EKS cluster to create a connection " +
65-
"between the cluster and RSC. See " + keySetupYAML + " for an alternative connection method.",
64+
Description: "Kubernetes `kubectl` command used to create a connection between the cluster and RSC. " +
65+
"See `" + keyManifest + "` for an alternative connection method.",
66+
},
67+
keyConnectionCommandExecuted: {
68+
Type: schema.TypeBool,
69+
Computed: true,
70+
Description: "Whether the connection command has been executed or the manifest has been applied.",
6671
},
6772
keyExocomputeID: {
6873
Type: schema.TypeString,
@@ -72,18 +77,26 @@ func resourceAwsExocomputeClusterAttachment() *schema.Resource {
7277
"created.",
7378
ValidateFunc: validation.IsUUID,
7479
},
80+
keyManifest: {
81+
Type: schema.TypeString,
82+
Computed: true,
83+
Description: "Kubernetes manifest which can be passed to `kubectl apply` to create a connection " +
84+
"between the cluster and RSC. See `" + keyConnectionCommand + "` for an alternative connection " +
85+
"method.",
86+
},
7587
keySetupYAML: {
7688
Type: schema.TypeString,
7789
Computed: true,
78-
Description: "K8s spec which can be passed to `kubectl apply` inside the EKS cluster to create a " +
79-
"connection between the cluster and RSC. See " + keyConnectionCommand + " for an alternative " +
80-
"connection method.",
90+
Description: "Kubernetes manifest which can be passed to `kubectl apply` to create a connection " +
91+
"between the cluster and RSC. See `" + keyConnectionCommand + "` for an alternative connection " +
92+
"method. **Deprecated:** use `manifest` instead.",
93+
Deprecated: "Use `manifest` instead.",
8194
},
8295
keyTokenRefresh: {
8396
Type: schema.TypeInt,
8497
Optional: true,
85-
Description: "To force a refresh of the token, part of the connection command, increase the value " +
86-
"of this field. The token is valid for 24 hours.",
98+
Description: "To force a refresh of the authentication token, part of the connection command and " +
99+
"manifest, increase the value of this field. The token is valid for 24 hours.",
87100
},
88101
},
89102
}
@@ -103,13 +116,23 @@ func awsCreateAwsExocomputeClusterAttachment(ctx context.Context, d *schema.Reso
103116
}
104117
clusterName := d.Get(keyClusterName).(string)
105118

106-
clusterID, kubectlCmd, setupYAML, err := aws.Wrap(client).AddClusterToExocomputeConfig(ctx, configID, clusterName)
119+
clusterID, kubectlCmd, setupYAML, err := aws.Wrap(client).AddExocomputeCluster(ctx, configID, clusterName)
107120
if err != nil {
108121
return diag.FromErr(err)
109122
}
123+
124+
// We initialize the resource fields in create instead of calling read,
125+
// this is because the read operation will fail until the customer runs the
126+
// connection command or applies the manifest.
110127
if err := d.Set(keyConnectionCommand, kubectlCmd); err != nil {
111128
return diag.FromErr(err)
112129
}
130+
if err := d.Set(keyConnectionCommandExecuted, false); err != nil {
131+
return diag.FromErr(err)
132+
}
133+
if err := d.Set(keyManifest, setupYAML); err != nil {
134+
return diag.FromErr(err)
135+
}
113136
if err := d.Set(keySetupYAML, setupYAML); err != nil {
114137
return diag.FromErr(err)
115138
}
@@ -121,8 +144,35 @@ func awsCreateAwsExocomputeClusterAttachment(ctx context.Context, d *schema.Reso
121144
func awsReadAwsExocomputeClusterAttachment(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
122145
log.Print("[TRACE] awsReadAwsExocomputeClusterAttachment")
123146

124-
// There is no way to read the state of the cluster attachment without
125-
// updating the token.
147+
client, err := m.(*client).polaris()
148+
if err != nil {
149+
return diag.FromErr(err)
150+
}
151+
152+
configID, err := uuid.Parse(d.Get(keyExocomputeID).(string))
153+
if err != nil {
154+
return diag.FromErr(err)
155+
}
156+
clusterName := d.Get(keyClusterName).(string)
157+
158+
kubectlCmd, setupYAML, err := aws.Wrap(client).ExocomputeCluster(ctx, configID, clusterName)
159+
if err != nil {
160+
log.Printf("[INFO] failed to read cluster attachment: %s", err)
161+
return nil
162+
}
163+
if err := d.Set(keyConnectionCommand, kubectlCmd); err != nil {
164+
return diag.FromErr(err)
165+
}
166+
if err := d.Set(keyConnectionCommandExecuted, true); err != nil {
167+
return diag.FromErr(err)
168+
}
169+
if err := d.Set(keyManifest, setupYAML); err != nil {
170+
return diag.FromErr(err)
171+
}
172+
if err := d.Set(keySetupYAML, setupYAML); err != nil {
173+
return diag.FromErr(err)
174+
}
175+
126176
return nil
127177
}
128178

internal/provider/resource_aws_exocompute_test.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,20 @@ resource "polaris_aws_account" "default" {
3636
profile = "{{ .Resource.Profile }}"
3737
3838
cloud_native_protection {
39+
permission_groups = [
40+
"BASIC",
41+
]
3942
regions = [
4043
"us-east-2",
4144
]
4245
}
4346
4447
exocompute {
48+
permission_groups = [
49+
"BASIC",
50+
"RSC_MANAGED_CLUSTER",
51+
]
52+
4553
regions = [
4654
"us-east-2",
4755
]
@@ -54,7 +62,7 @@ resource "polaris_aws_exocompute" "default" {
5462
vpc_id = "{{ .Resource.Exocompute.VPCID }}"
5563
5664
subnets = [
57-
{{ range .Resource.Exocompute.Subnets }}
65+
{{ range slice .Resource.Exocompute.Subnets 0 2 }}
5866
"{{ .ID }}",
5967
{{ end }}
6068
]
@@ -98,7 +106,7 @@ func TestAccPolarisAWSExocompute_basic(t *testing.T) {
98106
resource.TestCheckResourceAttr("polaris_aws_exocompute.default", "vpc_id", account.Exocompute.VPCID),
99107
resource.TestCheckResourceAttr("polaris_aws_exocompute.default", "polaris_managed", "true"),
100108
resource.TestCheckTypeSetElemAttr("polaris_aws_exocompute.default", "subnets.*", account.Exocompute.Subnets[0].ID),
101-
resource.TestCheckTypeSetElemAttr("polaris_aws_exocompute.default", "subnets.*", account.Exocompute.Subnets[0].ID),
109+
resource.TestCheckTypeSetElemAttr("polaris_aws_exocompute.default", "subnets.*", account.Exocompute.Subnets[1].ID),
102110
),
103111
}},
104112
})

0 commit comments

Comments
 (0)