/
resource_digitalocean_tag.go
113 lines (96 loc) · 2.72 KB
/
resource_digitalocean_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
package digitalocean
import (
"context"
"fmt"
"log"
"github.com/digitalocean/godo"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)
func resourceDigitalOceanTag() *schema.Resource {
return &schema.Resource{
Create: resourceDigitalOceanTagCreate,
Read: resourceDigitalOceanTagRead,
Delete: resourceDigitalOceanTagDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateTag,
},
"total_resource_count": {
Type: schema.TypeInt,
Computed: true,
},
"droplets_count": {
Type: schema.TypeInt,
Computed: true,
},
"images_count": {
Type: schema.TypeInt,
Computed: true,
},
"volumes_count": {
Type: schema.TypeInt,
Computed: true,
},
"volume_snapshots_count": {
Type: schema.TypeInt,
Computed: true,
},
"databases_count": {
Type: schema.TypeInt,
Computed: true,
},
},
}
}
func resourceDigitalOceanTagCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*CombinedConfig).godoClient()
// Build up our creation options
opts := &godo.TagCreateRequest{
Name: d.Get("name").(string),
}
log.Printf("[DEBUG] Tag create configuration: %#v", opts)
tag, _, err := client.Tags.Create(context.Background(), opts)
if err != nil {
return fmt.Errorf("Error creating tag: %s", err)
}
d.SetId(tag.Name)
log.Printf("[INFO] Tag: %s", tag.Name)
return resourceDigitalOceanTagRead(d, meta)
}
func resourceDigitalOceanTagRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*CombinedConfig).godoClient()
tag, resp, err := client.Tags.Get(context.Background(), d.Id())
if err != nil {
// If the tag is somehow already destroyed, mark as
// successfully gone
if resp != nil && resp.StatusCode == 404 {
d.SetId("")
return nil
}
return fmt.Errorf("Error retrieving tag: %s", err)
}
d.Set("name", tag.Name)
d.Set("total_resource_count", tag.Resources.Count)
d.Set("droplets_count", tag.Resources.Droplets.Count)
d.Set("images_count", tag.Resources.Images.Count)
d.Set("volumes_count", tag.Resources.Volumes.Count)
d.Set("volume_snapshots_count", tag.Resources.VolumeSnapshots.Count)
d.Set("databases_count", tag.Resources.Databases.Count)
return nil
}
func resourceDigitalOceanTagDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*CombinedConfig).godoClient()
log.Printf("[INFO] Deleting tag: %s", d.Id())
_, err := client.Tags.Delete(context.Background(), d.Id())
if err != nil {
return fmt.Errorf("Error deleting tag: %s", err)
}
d.SetId("")
return nil
}