/
services.go
122 lines (103 loc) · 3.1 KB
/
services.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
package provider
/*
Example resource usage:
resource "adaptive_servicelist" "example" {
name = "mydatabase256789"
urls = "comma,separated,urls"
}
*/
import (
"context"
"errors"
"time"
adaptive "github.com/adaptive-scale/terraform-provider-adaptive/internal/terraform-client"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"gopkg.in/yaml.v2"
)
type ServiceListIntegrationConfiguration struct {
Name string `yaml:"name"`
Version string `yaml:"version"`
URLs string `yaml:"urls"`
}
func resourceAdaptiveServiceList() *schema.Resource {
return &schema.Resource{
CreateContext: resourceAdaptiveServiceListCreate,
ReadContext: resourceAdaptiveServiceListRead,
UpdateContext: resourceAdaptiveServiceListUpdate,
DeleteContext: resourceAdaptiveServiceListDelete,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
Description: "The name of the service list to create.",
},
"urls": {
Type: schema.TypeString,
Required: true,
Description: "Comma-separated list of URLs.",
},
"last_updated": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
},
}
}
func schemaToServiceListIntegrationConfiguration(d *schema.ResourceData) ServiceListIntegrationConfiguration {
return ServiceListIntegrationConfiguration{
Version: "1",
Name: d.Get("name").(string),
URLs: d.Get("urls").(string),
}
}
func resourceAdaptiveServiceListCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*adaptive.Client)
obj := schemaToServiceListIntegrationConfiguration(d)
config, err := yaml.Marshal(obj)
if err != nil {
err := errors.New("provider error, could not marshal")
return diag.FromErr(err)
}
rName, err := nameFromSchema(d)
if err != nil {
return diag.FromErr(err)
}
resp, err := client.CreateResource(ctx, rName, "servicelist", config)
if err != nil {
return diag.FromErr(err)
}
d.SetId(resp.ID)
resourceAdaptiveServiceListRead(ctx, d, m)
return nil
}
func resourceAdaptiveServiceListRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
return nil
}
func resourceAdaptiveServiceListUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*adaptive.Client)
resourceID := d.Id()
obj := schemaToServiceListIntegrationConfiguration(d)
config, err := yaml.Marshal(obj)
if err != nil {
err := errors.New("provider error, could not marshal")
return diag.FromErr(err)
}
_, err = client.UpdateResource(resourceID, "servicelist", config)
if err != nil {
return diag.FromErr(err)
}
d.Set("last_updated", time.Now())
return resourceAdaptiveServiceListRead(ctx, d, m)
}
func resourceAdaptiveServiceListDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
resourceID := d.Id()
client := m.(*adaptive.Client)
_, err := client.DeleteResource(resourceID, d.Get("name").(string))
if err != nil {
return diag.FromErr(err)
}
d.SetId("")
return nil
}