generated from hashicorp/terraform-provider-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 40
/
resource_project.go
124 lines (100 loc) · 3.66 KB
/
resource_project.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
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/harness/terraform-provider-harness/internal/utils"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func ResourceProject() *schema.Resource {
resource := &schema.Resource{
Description: "Resource for creating a Harness project.",
ReadContext: resourceProjectRead,
UpdateContext: resourceProjectCreateOrUpdate,
DeleteContext: resourceProjectDelete,
CreateContext: resourceProjectCreateOrUpdate,
Importer: helpers.OrgResourceImporter,
Schema: map[string]*schema.Schema{
"color": {
Description: "Color of the project.",
Type: schema.TypeString,
Optional: true,
},
"modules": {
Description: "Modules in the project.",
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
}
helpers.SetOrgLevelResourceSchema(resource.Schema)
return resource
}
func resourceProjectRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c := meta.(*internal.Session).PLClient
id := d.Id()
resp, _, err := c.ProjectApi.GetProject(ctx, id, c.AccountId, &nextgen.ProjectApiGetProjectOpts{
OrgIdentifier: optional.NewString(d.Get("org_id").(string)),
})
if err != nil {
return helpers.HandleApiError(err, d)
}
readProject(d, resp.Data.Project)
return nil
}
func resourceProjectCreateOrUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c := meta.(*internal.Session).PLClient
var err error
var resp nextgen.ResponseDtoProjectResponse
id := d.Id()
project := buildProject(d)
if id == "" {
resp, _, err = c.ProjectApi.PostProject(ctx, nextgen.ProjectRequest{Project: project}, c.AccountId, &nextgen.ProjectApiPostProjectOpts{
OrgIdentifier: optional.NewString(d.Get("org_id").(string)),
})
} else {
resp, _, err = c.ProjectApi.PutProject(ctx, nextgen.ProjectRequest{Project: project}, c.AccountId, id, &nextgen.ProjectApiPutProjectOpts{
OrgIdentifier: optional.NewString(d.Get("org_id").(string)),
})
}
if err != nil {
return diag.Errorf(err.(nextgen.GenericSwaggerError).Error())
}
readProject(d, resp.Data.Project)
return nil
}
func resourceProjectDelete(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 buildProject(d *schema.ResourceData) *nextgen.Project {
return &nextgen.Project{
Identifier: d.Get("identifier").(string),
OrgIdentifier: d.Get("org_id").(string),
Name: d.Get("name").(string),
Color: d.Get("color").(string),
Description: d.Get("description").(string),
Modules: utils.InterfaceSliceToStringSlice(d.Get("modules").(*schema.Set).List()),
Tags: helpers.ExpandTags(d.Get("tags").(*schema.Set).List()),
}
}
func readProject(d *schema.ResourceData, project *nextgen.Project) {
d.SetId(project.Identifier)
d.Set("identifier", project.Identifier)
d.Set("org_id", project.OrgIdentifier)
d.Set("name", project.Name)
d.Set("color", project.Color)
d.Set("description", project.Description)
d.Set("modules", project.Modules)
d.Set("tags", helpers.FlattenTags(project.Tags))
}