/
awsdocumentdb.go
112 lines (95 loc) · 2.98 KB
/
awsdocumentdb.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
package provider
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"
)
func resourceAdaptiveDocumentDB() *schema.Resource {
return &schema.Resource{
CreateContext: resourceAdaptiveMongoCreate,
ReadContext: resourceAdaptiveMongoRead,
UpdateContext: resourceAdaptiveMongoUpdate,
DeleteContext: resourceAdaptiveMongoDelete,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
Description: "The name of the MongoDB instance to create.",
},
"uri": {
Type: schema.TypeString,
Required: true,
Description: "The connection string for the MongoDB instance to connect to.",
},
"last_updated": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
},
}
}
type AWSDocumentDBIntegrationConfiguration struct {
Name string `yaml:"name"`
URI string `yaml:"uri"`
}
// TODO: .(string) is assumption will cause problems
func schemaToAWSDocumentDBIntegrationConfiguration(d *schema.ResourceData) AWSDocumentDBIntegrationConfiguration {
return AWSDocumentDBIntegrationConfiguration{
Name: d.Get("name").(string),
URI: d.Get("uri").(string),
}
}
func resourceAdaptiveAWSDocumentDBCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*adaptive.Client)
obj := schemaToAWSDocumentDBIntegrationConfiguration(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, "awsdocumentdb", config)
if err != nil {
return diag.FromErr(err)
}
d.SetId(resp.ID)
resourceAdaptiveAWSDocumentDBRead(ctx, d, m)
return nil
}
func resourceAdaptiveAWSDocumentDBRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
return nil
}
func resourceAdaptiveAWSDocumentDBUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*adaptive.Client)
resourceID := d.Id()
obj := schemaToAWSDocumentDBIntegrationConfiguration(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, "awsdocumentdb", config)
if err != nil {
return diag.FromErr(err)
}
d.Set("last_updated", time.Now())
return resourceAdaptiveAWSDocumentDBRead(ctx, d, m)
}
func resourceAdaptiveAWSDocumentDBDelete(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
}