/
mysql_aws_secrets_manager.go
137 lines (118 loc) · 3.55 KB
/
mysql_aws_secrets_manager.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package provider
/*
Example resource usage:
resource "adaptive_mysql" "example" {
name = "mydatabase256789"
database_name = ""
host = "myhost.example.com"
port = "5433"
username = "myuser"
password = "mypasswor2"
}
*/
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 MySQLAWSIntegrationConfiguration struct {
Version string `yaml:"version"`
Name string `yaml:"name"`
ARN string `yaml:"arn"`
Region string `yaml:"region"`
SecretID string `yaml:"secret_id"`
}
func resourceAdaptiveMyAWSSQL() *schema.Resource {
return &schema.Resource{
CreateContext: resourceAdaptiveMySQLAWSCreate,
ReadContext: resourceAdaptiveMySQLAWSRead,
UpdateContext: resourceAdaptiveMySQLAWSUpdate,
DeleteContext: resourceAdaptiveMySQLAWSDelete,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
Description: "Integration name for MySQL.",
},
"arn": {
Type: schema.TypeString,
Optional: true,
Default: "",
Description: "ARN for the Assumed Role.",
},
"region": {
Type: schema.TypeString,
Required: true,
Description: "AWS region of the secrets manager secret.",
},
"secret_id": {
Type: schema.TypeString,
Required: true,
Description: "Secret ID of the secrets manager secret.",
},
},
}
}
// TODO: .(string) is assumption will cause problems
func schemaToMySQLAWSIntegrationConfiguration(d *schema.ResourceData) MySQLAWSIntegrationConfiguration {
return MySQLAWSIntegrationConfiguration{
Version: "",
Name: d.Get("name").(string),
ARN: d.Get("arn").(string),
Region: d.Get("region").(string),
SecretID: d.Get("secret_id").(string),
}
}
func resourceAdaptiveMySQLAWSCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*adaptive.Client)
obj := schemaToMySQLAWSIntegrationConfiguration(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, "mysql", config)
if err != nil {
return diag.FromErr(err)
}
d.SetId(resp.ID)
resourceAdaptiveMySQLAWSRead(ctx, d, m)
return nil
}
func resourceAdaptiveMySQLAWSRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
return nil
}
func resourceAdaptiveMySQLAWSUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*adaptive.Client)
resourceID := d.Id()
obj := schemaToMySQLAWSIntegrationConfiguration(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, "mysql", config)
if err != nil {
return diag.FromErr(err)
}
d.Set("last_updated", time.Now())
return resourceAdaptiveMySQLAWSUpdate(ctx, d, m)
}
func resourceAdaptiveMySQLAWSDelete(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
}