/
product_type_resource.go
141 lines (121 loc) · 4.79 KB
/
product_type_resource.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
package provider
import (
"context"
dd "github.com/doximity/defect-dojo-client-go"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/types"
)
func (t productTypeResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
// This description is used by the documentation generator and the language server.
MarkdownDescription: "DefectDojo Product Type",
Attributes: map[string]schema.Attribute{
"name": schema.StringAttribute{
MarkdownDescription: "The name of the Product Type",
Required: true,
},
"description": schema.StringAttribute{
MarkdownDescription: "The description of the Product Type",
Optional: true,
Computed: true,
PlanModifiers: []planmodifier.String{
stringDefault(""),
},
},
"critical_product": schema.BoolAttribute{
MarkdownDescription: "Is this a critical Product Type",
Optional: true,
Computed: true,
PlanModifiers: []planmodifier.Bool{
boolDefault(false),
},
},
"key_product": schema.BoolAttribute{
MarkdownDescription: "Is this a key Product Type",
Optional: true,
Computed: true,
PlanModifiers: []planmodifier.Bool{
boolDefault(false),
},
},
"id": schema.StringAttribute{ // the id (for import purposes) MUST be a string
Computed: true,
MarkdownDescription: "Identifier",
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
},
}
}
type productTypeResourceData struct {
Name types.String `tfsdk:"name" ddField:"Name"`
Description types.String `tfsdk:"description" ddField:"Description"`
CriticalProduct types.Bool `tfsdk:"critical_product" ddField:"CriticalProduct"`
KeyProduct types.Bool `tfsdk:"key_product" ddField:"KeyProduct"`
Id types.String `tfsdk:"id" ddField:"Id"`
}
type productTypeDefectdojoResource struct {
dd.ProductType
}
func (ddr *productTypeDefectdojoResource) createApiCall(ctx context.Context, client *dd.ClientWithResponses) (int, []byte, error) {
reqBody := dd.ProductTypesCreateJSONRequestBody(ddr.ProductType)
apiResp, err := client.ProductTypesCreateWithResponse(ctx, reqBody)
if apiResp.JSON201 != nil {
ddr.ProductType = *apiResp.JSON201
}
return apiResp.StatusCode(), apiResp.Body, err
}
func (ddr *productTypeDefectdojoResource) readApiCall(ctx context.Context, client *dd.ClientWithResponses, idNumber int) (int, []byte, error) {
apiResp, err := client.ProductTypesRetrieveWithResponse(ctx, idNumber, &dd.ProductTypesRetrieveParams{})
if apiResp.JSON200 != nil {
ddr.ProductType = *apiResp.JSON200
}
return apiResp.StatusCode(), apiResp.Body, err
}
func (ddr *productTypeDefectdojoResource) updateApiCall(ctx context.Context, client *dd.ClientWithResponses, idNumber int) (int, []byte, error) {
reqBody := dd.ProductTypesUpdateJSONRequestBody(ddr.ProductType)
apiResp, err := client.ProductTypesUpdateWithResponse(ctx, idNumber, reqBody)
if apiResp.JSON200 != nil {
ddr.ProductType = *apiResp.JSON200
}
return apiResp.StatusCode(), apiResp.Body, err
}
func (ddr *productTypeDefectdojoResource) deleteApiCall(ctx context.Context, client *dd.ClientWithResponses, idNumber int) (int, []byte, error) {
apiResp, err := client.ProductTypesDestroyWithResponse(ctx, idNumber)
return apiResp.StatusCode(), apiResp.Body, err
}
type productTypeResource struct {
terraformResource
}
// Ensure provider defined types fully satisfy framework interfaces
var _ resource.Resource = &productTypeResource{}
var _ resource.ResourceWithImportState = &productTypeResource{}
func NewProductTypeResource() resource.Resource {
return &productTypeResource{
terraformResource: terraformResource{
dataProvider: productTypeDataProvider{},
},
}
}
func (r productTypeResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_product_type"
}
type productTypeDataProvider struct{}
func (r productTypeDataProvider) getData(ctx context.Context, getter dataGetter) (terraformResourceData, diag.Diagnostics) {
var data productTypeResourceData
diags := getter.Get(ctx, &data)
return &data, diags
}
func (d *productTypeResourceData) id() types.String {
return d.Id
}
func (d *productTypeResourceData) defectdojoResource() defectdojoResource {
return &productTypeDefectdojoResource{
ProductType: dd.ProductType{},
}
}