/
resource_scalr_tag.go
119 lines (101 loc) · 3.06 KB
/
resource_scalr_tag.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
package scalr
import (
"context"
"errors"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/scalr/go-scalr"
"log"
)
func resourceScalrTag() *schema.Resource {
return &schema.Resource{
Description: "Manages the state of tags in Scalr.",
CreateContext: resourceScalrTagCreate,
ReadContext: resourceScalrTagRead,
UpdateContext: resourceScalrTagUpdate,
DeleteContext: resourceScalrTagDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
"name": {
Description: "Name of the tag.",
Type: schema.TypeString,
Required: true,
},
"account_id": {
Description: "ID of the account, in the format `acc-<RANDOM STRING>`.",
Type: schema.TypeString,
Optional: true,
Computed: true,
DefaultFunc: scalrAccountIDDefaultFunc,
ForceNew: true,
},
},
}
}
func resourceScalrTagRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
scalrClient := meta.(*scalr.Client)
id := d.Id()
log.Printf("[DEBUG] Read tag: %s", id)
tag, err := scalrClient.Tags.Read(ctx, id)
if err != nil {
if errors.Is(err, scalr.ErrResourceNotFound) {
log.Printf("[DEBUG] Tag %s not found", id)
d.SetId("")
return nil
}
return diag.Errorf("Error reading tag %s: %v", id, err)
}
// Update config.
_ = d.Set("name", tag.Name)
_ = d.Set("account_id", tag.Account.ID)
return nil
}
func resourceScalrTagCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
scalrClient := meta.(*scalr.Client)
// Get the name and account_id.
name := d.Get("name").(string)
accountID := d.Get("account_id").(string)
options := scalr.TagCreateOptions{
Name: scalr.String(name),
Account: &scalr.Account{ID: accountID},
}
log.Printf("[DEBUG] Create tag %s for account %s", name, accountID)
tag, err := scalrClient.Tags.Create(ctx, options)
if err != nil {
return diag.Errorf(
"Error creating tag %s for account %s: %v", name, accountID, err)
}
d.SetId(tag.ID)
return resourceScalrTagRead(ctx, d, meta)
}
func resourceScalrTagUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
scalrClient := meta.(*scalr.Client)
id := d.Id()
if d.HasChange("name") {
name := d.Get("name").(string)
opts := scalr.TagUpdateOptions{
Name: scalr.String(name),
}
log.Printf("[DEBUG] Update tag %s", id)
_, err := scalrClient.Tags.Update(ctx, id, opts)
if err != nil {
return diag.Errorf("error updating tag %s: %v", id, err)
}
}
return resourceScalrTagRead(ctx, d, meta)
}
func resourceScalrTagDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
scalrClient := meta.(*scalr.Client)
id := d.Id()
log.Printf("[DEBUG] Delete tag %s", id)
err := scalrClient.Tags.Delete(ctx, id)
if err != nil {
if errors.Is(err, scalr.ErrResourceNotFound) {
return nil
}
return diag.Errorf("Error deleting tag %s: %v", id, err)
}
return nil
}