generated from hashicorp/terraform-provider-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 41
/
resource_cluster.go
134 lines (113 loc) · 3.95 KB
/
resource_cluster.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package cluster
import (
"context"
"net/http"
"github.com/antihax/optional"
"github.com/harness/harness-go-sdk/harness/nextgen"
"github.com/harness/terraform-provider-harness/helpers"
"github.com/harness/terraform-provider-harness/internal"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func ResourceCluster() *schema.Resource {
resource := &schema.Resource{
Description: "Resource for creating a Harness Cluster.",
ReadContext: resourceClusterRead,
DeleteContext: resourceClusterDelete,
CreateContext: resourceClusterLink,
Importer: helpers.ProjectResourceImporter,
Schema: map[string]*schema.Schema{
"identifier": {
Description: "identifier of the cluster.",
Type: schema.TypeString,
Required: true,
},
"envref": {
Description: "environment identifier of the cluster.",
Type: schema.TypeString,
Required: true,
},
"org_id": {
Description: "org_id of the cluster.",
Type: schema.TypeString,
Optional: true,
},
"project_id": {
Description: "project_id of the cluster.",
Type: schema.TypeString,
Optional: true,
},
"scope": {
Description: "scope at which the cluster exists in harness gitops",
Type: schema.TypeString,
Optional: true,
},
},
}
return resource
}
func resourceClusterRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c, ctx := meta.(*internal.Session).GetPlatformClientWithContext(ctx)
envId := d.Get("env_id").(string)
resp, httpResp, err := c.ClustersApi.GetCluster(ctx, d.Id(), c.AccountId, envId, &nextgen.ClustersApiGetClusterOpts{
OrgIdentifier: optional.NewString(d.Get("org_id").(string)),
ProjectIdentifier: optional.NewString(d.Get("project_id").(string)),
})
if err != nil {
return helpers.HandleApiError(err, d, httpResp)
}
// Soft delete lookup error handling
// https://harness.atlassian.net/browse/PL-23765
if resp.Data == nil {
d.SetId("")
d.MarkNewResource()
return nil
}
readCluster(d, resp.Data)
return nil
}
func resourceClusterDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c, ctx := meta.(*internal.Session).GetPlatformClientWithContext(ctx)
envId := d.Get("env_id").(string)
_, httpResp, err := c.ClustersApi.DeleteCluster(ctx, d.Id(), c.AccountId, envId, &nextgen.ClustersApiDeleteClusterOpts{
OrgIdentifier: optional.NewString(d.Get("org_id").(string)),
ProjectIdentifier: optional.NewString(d.Get("project_id").(string)),
})
if err != nil {
return helpers.HandleApiError(err, d, httpResp)
}
return nil
}
func resourceClusterLink(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c, ctx := meta.(*internal.Session).GetPlatformClientWithContext(ctx)
var err error
var resp nextgen.ResponseDtoClusterResponse
var httpResp *http.Response
env := buildCluster(d)
resp, httpResp, err = c.ClustersApi.LinkCluster(ctx, c.AccountId, &nextgen.ClustersApiLinkClusterOpts{
Body: optional.NewInterface(env),
})
if err != nil {
return helpers.HandleApiError(err, d, httpResp)
}
readCluster(d, resp.Data)
return nil
}
func buildCluster(d *schema.ResourceData) *nextgen.ClusterRequest {
return &nextgen.ClusterRequest{
Identifier: d.Get("identifier").(string),
OrgIdentifier: d.Get("org_id").(string),
ProjectIdentifier: d.Get("project_id").(string),
EnvRef: d.Get("envref").(string),
Scope: d.Get("scope").(string),
}
}
func readCluster(d *schema.ResourceData, cl *nextgen.ClusterResponse) {
d.Set("clusterRef", cl.ClusterRef)
d.Set("org_id", cl.OrgIdentifier)
d.Set("proj_id", cl.ProjectIdentifier)
d.Set("account_id", cl.AccountIdentifier)
d.Set("envref", cl.EnvRef)
d.Set("scope", cl.Scope)
d.Set("name", cl.Name)
}