generated from hashicorp/terraform-provider-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 40
/
resource_service.go
114 lines (89 loc) · 3.3 KB
/
resource_service.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
package platform
import (
"context"
"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 ResourceService() *schema.Resource {
resource := &schema.Resource{
Description: "Resource for creating a Harness project.",
ReadContext: resourceServiceRead,
UpdateContext: resourceServiceCreateOrUpdate,
DeleteContext: resourceServiceDelete,
CreateContext: resourceServiceCreateOrUpdate,
Importer: helpers.ProjectResourceImporter,
Schema: map[string]*schema.Schema{},
}
helpers.SetProjectLevelResourceSchema(resource.Schema)
return resource
}
func resourceServiceRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c := meta.(*internal.Session).PLClient
id := d.Id()
resp, _, err := c.ServicesApi.GetServiceV2(ctx, id, c.AccountId, &nextgen.ServicesApiGetServiceV2Opts{
OrgIdentifier: optional.NewString(d.Get("org_id").(string)),
ProjectIdentifier: optional.NewString(d.Get("project_id").(string)),
})
if err != nil {
return helpers.HandleApiError(err, d)
}
if resp.Data == nil || resp.Data.Service == nil {
d.SetId("")
d.MarkNewResource()
return nil
}
readService(d, resp.Data.Service)
return nil
}
func resourceServiceCreateOrUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c := meta.(*internal.Session).PLClient
var err error
var resp nextgen.ResponseDtoServiceResponse
id := d.Id()
svc := buildService(d)
if id == "" {
resp, _, err = c.ServicesApi.CreateServiceV2(ctx, c.AccountId, &nextgen.ServicesApiCreateServiceV2Opts{
Body: optional.NewInterface(svc),
})
} else {
resp, _, err = c.ServicesApi.UpdateServiceV2(ctx, c.AccountId, &nextgen.ServicesApiUpdateServiceV2Opts{
Body: optional.NewInterface(svc),
})
}
if err != nil {
return diag.Errorf(err.(nextgen.GenericSwaggerError).Error())
}
readService(d, resp.Data.Service)
return nil
}
func resourceServiceDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c := meta.(*internal.Session).PLClient
_, _, err := c.ProjectApi.DeleteProject(ctx, d.Id(), c.AccountId, &nextgen.ProjectApiDeleteProjectOpts{OrgIdentifier: optional.NewString(d.Get("org_id").(string))})
if err != nil {
return diag.Errorf(err.(nextgen.GenericSwaggerError).Error())
}
return nil
}
func buildService(d *schema.ResourceData) *nextgen.ServiceRequest {
return &nextgen.ServiceRequest{
Identifier: d.Get("identifier").(string),
OrgIdentifier: d.Get("org_id").(string),
ProjectIdentifier: d.Get("project_id").(string),
Name: d.Get("name").(string),
Description: d.Get("description").(string),
Tags: helpers.ExpandTags(d.Get("tags").(*schema.Set).List()),
}
}
func readService(d *schema.ResourceData, project *nextgen.ServiceRequest) {
d.SetId(project.Identifier)
d.Set("identifier", project.Identifier)
d.Set("org_id", project.OrgIdentifier)
d.Set("project_id", project.ProjectIdentifier)
d.Set("name", project.Name)
d.Set("description", project.Description)
d.Set("tags", helpers.FlattenTags(project.Tags))
}