-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource.go
120 lines (92 loc) · 3.05 KB
/
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
// Copyright 2022 Nordcloud Oy or its affiliates. All Rights Reserved.
package variable
import (
"context"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/nordcloud/terraform-provider-imagefactory/pkg/config"
"github.com/nordcloud/terraform-provider-imagefactory/pkg/graphql"
"github.com/nordcloud/terraform-provider-imagefactory/pkg/helper/mutexkv"
"github.com/nordcloud/terraform-provider-imagefactory/pkg/sdk"
)
// This is a global MutexKV for use within this plugin.
var variableMutexKV = mutexkv.NewMutexKV("variable")
func Resource() *schema.Resource {
return &schema.Resource{
CreateContext: create,
ReadContext: read,
UpdateContext: update,
DeleteContext: delete,
Schema: variableSchema,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
}
}
func create(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
c := m.(*config.Config)
variableName := d.Get("name").(string)
variableValue := d.Get("value").(string)
variableMutexKV.Lock(ctx, variableName)
defer variableMutexKV.Unlock(ctx, variableName)
input := sdk.NewVariable{
Name: graphql.String(variableName),
Value: graphql.String(variableValue),
}
variable, err := c.APIClient.CreateVariable(input)
if err != nil {
return diag.FromErr(err)
}
return setProps(d, variable)
}
func read(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { // nolint: dupl
c := m.(*config.Config)
variableName := d.Get("name").(string)
variable, err := c.APIClient.GetVariable(variableName)
if err != nil {
return diag.FromErr(err)
}
return setProps(d, variable)
}
func update(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { // nolint: dupl
c := m.(*config.Config)
o, n := d.GetChange("name")
oldVariableName := o.(string)
newVariableName := n.(string)
variableValue := d.Get("value").(string)
variableMutexKV.Lock(ctx, newVariableName)
defer variableMutexKV.Unlock(ctx, newVariableName)
if oldVariableName != newVariableName {
if err := c.APIClient.DeleteVariable(oldVariableName); err != nil {
return diag.FromErr(err)
}
}
input := sdk.NewVariable{
Name: graphql.String(newVariableName),
Value: graphql.String(variableValue),
}
variable, err := c.APIClient.UpdateVariable(input)
if err != nil {
return diag.FromErr(err)
}
return setProps(d, variable)
}
func delete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var diags diag.Diagnostics
c := m.(*config.Config)
variableName := d.Get("name").(string)
variableMutexKV.Lock(ctx, variableName)
defer variableMutexKV.Unlock(ctx, variableName)
if err := c.APIClient.DeleteVariable(variableName); err != nil {
return diag.FromErr(err)
}
return diags
}
func setProps(d *schema.ResourceData, v sdk.Variable) diag.Diagnostics {
var diags diag.Diagnostics
d.SetId(string(v.Hash))
if err := d.Set("name", v.Name); err != nil {
return diag.FromErr(err)
}
return diags
}