• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

itsmechlark / terraform-provider-cloud66 / 6201628089

15 Sep 2023 06:34PM UTC coverage: 79.757%. Remained the same
6201628089

push

github

itsmechlark
ci: Configure golangci-lint

8 of 16 new or added lines in 5 files covered. (50.0%)

10 existing lines in 1 file now uncovered.

721 of 904 relevant lines covered (79.76%)

6.47 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

60.66
/cloud66/resource_cloud66_env_variable.go
1
package cloud66
2

3
import (
4
        "fmt"
5
        "log"
6
        "strings"
7
        "time"
8

9
        "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
10
        api "github.com/itsmechlark/cloud66"
11
)
12

13
func resourceCloud66EnvVariable() *schema.Resource {
9✔
14
        return &schema.Resource{
9✔
15
                Create: resourceCloud66EnvVariableCreate,
9✔
16
                Read:   resourceCloud66EnvVariableRead,
9✔
17
                Update: resourceCloud66EnvVariableUpdate,
9✔
18
                Delete: resourceCloud66EnvVariableDelete,
9✔
19
                Importer: &schema.ResourceImporter{
9✔
20
                        State: resourceCloud66EnvVariableImport,
9✔
21
                },
9✔
22

9✔
23
                SchemaVersion: 2,
9✔
24
                Schema:        resourceCloud66EnvVariableSchema(),
9✔
25
                Timeouts: &schema.ResourceTimeout{
9✔
26
                        Create: schema.DefaultTimeout(30 * time.Second),
9✔
27
                        Update: schema.DefaultTimeout(30 * time.Second),
9✔
28
                },
9✔
29
        }
9✔
30
}
9✔
31

32
func resourceCloud66EnvVariableCreate(d *schema.ResourceData, meta interface{}) error {
2✔
33
        providerConfig := meta.(ProviderConfig)
2✔
34
        client := providerConfig.client
2✔
35

2✔
36
        stackID := d.Get("stack_id").(string)
2✔
37
        key := d.Get("key").(string)
2✔
38
        value := d.Get("value").(string)
2✔
39
        applyStrategy := d.Get("apply_strategy").(string)
2✔
40

2✔
41
        log.Printf("[INFO] Creating %s Env Variable for stack %s", key, stackID)
2✔
42

2✔
43
        record, err := client.StackEnvVarNew(stackID, key, value, applyStrategy)
2✔
44

2✔
45
        if record == nil {
2✔
46
                return fmt.Errorf("error creating Env Variable %q: %s", stackID, err)
×
47
        }
×
48

49
        envVar := api.StackEnvVar{
2✔
50
                Key:      key,
2✔
51
                Value:    value,
2✔
52
                Readonly: d.Get("readonly").(bool),
2✔
53
        }
2✔
54

2✔
55
        setCloud66EnvVariableData(d, &envVar)
2✔
56

2✔
57
        return nil
2✔
58
}
59

60
func resourceCloud66EnvVariableRead(d *schema.ResourceData, meta interface{}) error {
7✔
61
        providerConfig := meta.(ProviderConfig)
7✔
62
        client := providerConfig.client
7✔
63

7✔
64
        stackID := d.Get("stack_id").(string)
7✔
65
        key := d.Id()
7✔
66

7✔
67
        records, err := client.StackEnvVars(stackID)
7✔
68
        if records != nil {
14✔
69
                for _, record := range records {
14✔
70
                        if record.Key == key {
14✔
71
                                setCloud66EnvVariableData(d, &record)
7✔
72
                                break
7✔
73
                        }
74
                }
75
        } else {
×
76
                return fmt.Errorf("error reading Env Variable %q: %s", stackID, err)
×
77
        }
×
78

79
        return nil
7✔
80
}
81

82
func resourceCloud66EnvVariableUpdate(d *schema.ResourceData, meta interface{}) error {
×
83
        providerConfig := meta.(ProviderConfig)
×
84
        client := providerConfig.client
×
85

×
86
        stackID := d.Get("stack_id").(string)
×
87
        key := d.Id()
×
88
        value := d.Get("value").(string)
×
89
        applyStrategy := d.Get("apply_strategy").(string)
×
90

×
91
        log.Printf("[INFO] Updating %s Env Variable for stack %s", key, stackID)
×
92

×
93
        record, err := client.StackEnvVarSet(stackID, key, value, applyStrategy)
×
94

×
95
        if record == nil {
×
96
                return fmt.Errorf("error updating Env Variable %q: %s", stackID, err)
×
97
        }
×
98

99
        envVar := api.StackEnvVar{
×
100
                Key:      key,
×
101
                Value:    value,
×
102
                Readonly: d.Get("readonly").(bool),
×
103
        }
×
104

×
105
        setCloud66EnvVariableData(d, &envVar)
×
106

×
107
        return nil
×
108
}
109

110
func resourceCloud66EnvVariableDelete(d *schema.ResourceData, meta interface{}) error {
2✔
111
        providerConfig := meta.(ProviderConfig)
2✔
112
        client := providerConfig.client
2✔
113

2✔
114
        stackID := d.Get("stack_id").(string)
2✔
115
        key := d.Id()
2✔
116

2✔
117
        req, err := client.NewRequest("DELETE", "/stacks/"+stackID+"/environments/"+key+".json", nil, nil)
2✔
118
        if req == nil {
2✔
119
                return fmt.Errorf("error deleting Env Variable %q: %s", stackID, err)
×
120
        }
×
121

122
        return nil
2✔
123
}
124

125
func resourceCloud66EnvVariableImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
×
126
        // split the id so we can lookup
×
127
        idAttr := strings.SplitN(d.Id(), "/", 2)
×
128
        if len(idAttr) != 2 {
×
129
                return nil, fmt.Errorf("invalid id (\"%s\") specified, should be in format \"stackID/key\"", d.Id())
×
130
        }
×
131

132
        stackID, key := idAttr[0], idAttr[1]
×
133

×
134
        log.Printf("[DEBUG] Importing %s Env Variable for stack %s", key, stackID)
×
135

×
136
        d.Set("stack_id", stackID)
×
137
        d.SetId(key)
×
138

×
NEW
139
        resourceCloud66EnvVariableRead(d, meta) //nolint:golint,errcheck
×
140

×
141
        return []*schema.ResourceData{d}, nil
×
142
}
143

144
func setCloud66EnvVariableData(d *schema.ResourceData, envVar *api.StackEnvVar) {
9✔
145
        stackID := d.Get("stack_id").(string)
9✔
146

9✔
147
        d.SetId(envVar.Key)
9✔
148
        d.Set("stack_id", stackID)
9✔
149
        d.Set("key", envVar.Key)
9✔
150
        d.Set("value", envVar.Value)
9✔
151
        d.Set("readonly", envVar.Readonly)
9✔
152
}
9✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc