generated from hashicorp/terraform-provider-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 41
/
resource_Infrastructure.go
150 lines (125 loc) · 4.94 KB
/
resource_Infrastructure.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package infrastructure
import (
"context"
"fmt"
"net/http"
"strings"
"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 ResourceInfrastructure() *schema.Resource {
resource := &schema.Resource{
Description: "Data source for retrieving a Harness Infrastructure.",
ReadContext: resourceInfrastructureRead,
UpdateContext: resourceInfrastructureCreateOrUpdate,
DeleteContext: resourceInfrastructureDelete,
CreateContext: resourceInfrastructureCreateOrUpdate,
Importer: helpers.EnvRelatedResourceImporter,
Schema: map[string]*schema.Schema{
"identifier": {
Description: "identifier of the Infrastructure.",
Type: schema.TypeString,
Required: true,
},
"env_id": {
Description: "environment identifier.",
Type: schema.TypeString,
Required: true,
},
"type": {
Description: fmt.Sprintf("Type of Infrastructure. Valid values are %s.", strings.Join(nextgen.InfrastructureTypeValues, ", ")),
Type: schema.TypeString,
Required: true,
},
"yaml": {
Description: "Infrastructure YAML",
Type: schema.TypeString,
Required: true,
},
"deployment_type": {
Description: fmt.Sprintf("Infrastructure deployment type. Valid values are %s.", strings.Join(nextgen.InfrastructureDeploymentypeValues, ", ")),
Type: schema.TypeString,
Optional: true,
Computed: true,
},
},
}
helpers.SetProjectLevelResourceSchema(resource.Schema)
return resource
}
func resourceInfrastructureRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c, ctx := meta.(*internal.Session).GetPlatformClientWithContext(ctx)
env_id := d.Get("env_id").(string)
resp, httpResp, err := c.InfrastructuresApi.GetInfrastructure(ctx, d.Id(), c.AccountId, env_id, &nextgen.InfrastructuresApiGetInfrastructureOpts{
OrgIdentifier: optional.NewString(d.Get("org_id").(string)),
ProjectIdentifier: optional.NewString(d.Get("project_id").(string)),
})
if err != nil {
return helpers.HandleReadApiError(err, d, httpResp)
}
readInfrastructure(d, resp.Data)
return nil
}
func resourceInfrastructureCreateOrUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c, ctx := meta.(*internal.Session).GetPlatformClientWithContext(ctx)
var err error
var resp nextgen.ResponseDtoInfrastructureResponse
var httpResp *http.Response
id := d.Id()
infra := buildInfrastructure(d)
if id == "" {
resp, httpResp, err = c.InfrastructuresApi.CreateInfrastructure(ctx, c.AccountId, &nextgen.InfrastructuresApiCreateInfrastructureOpts{
Body: optional.NewInterface(infra),
})
} else {
resp, httpResp, err = c.InfrastructuresApi.UpdateInfrastructure(ctx, c.AccountId, &nextgen.InfrastructuresApiUpdateInfrastructureOpts{
Body: optional.NewInterface(infra),
})
}
if err != nil {
return helpers.HandleApiError(err, d, httpResp)
}
readInfrastructure(d, resp.Data)
return nil
}
func resourceInfrastructureDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c, ctx := meta.(*internal.Session).GetPlatformClientWithContext(ctx)
env_id := d.Get("env_id").(string)
_, httpResp, err := c.InfrastructuresApi.DeleteInfrastructure(ctx, d.Id(), c.AccountId, env_id, &nextgen.InfrastructuresApiDeleteInfrastructureOpts{
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 buildInfrastructure(d *schema.ResourceData) *nextgen.InfrastructureRequest {
return &nextgen.InfrastructureRequest{
Identifier: d.Get("identifier").(string),
OrgIdentifier: d.Get("org_id").(string),
ProjectIdentifier: d.Get("project_id").(string),
EnvironmentRef: d.Get("env_id").(string),
Name: d.Get("name").(string),
Description: d.Get("description").(string),
Tags: helpers.ExpandTags(d.Get("tags").(*schema.Set).List()),
Type_: d.Get("type").(string),
Yaml: d.Get("yaml").(string),
}
}
func readInfrastructure(d *schema.ResourceData, infra *nextgen.InfrastructureResponse) {
d.SetId(infra.Infrastructure.Identifier)
d.Set("org_id", infra.Infrastructure.OrgIdentifier)
d.Set("project_id", infra.Infrastructure.ProjectIdentifier)
d.Set("env_id", infra.Infrastructure.EnvironmentRef)
d.Set("name", infra.Infrastructure.Name)
d.Set("description", infra.Infrastructure.Description)
d.Set("tags", helpers.FlattenTags(infra.Infrastructure.Tags))
d.Set("type", infra.Infrastructure.Type_)
d.Set("deployment_type", infra.Infrastructure.DeploymentType)
d.Set("yaml", infra.Infrastructure.Yaml)
}