This repository has been archived by the owner on Aug 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
resource_opentelekomcloud_rts_software_config_v1.go
173 lines (150 loc) · 4.65 KB
/
resource_opentelekomcloud_rts_software_config_v1.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package opentelekomcloud
import (
"fmt"
"log"
"time"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/huaweicloud/golangsdk"
"github.com/huaweicloud/golangsdk/openstack/rts/v1/softwareconfig"
)
func resourceSoftwareConfigV1() *schema.Resource {
return &schema.Resource{
Create: resourceSoftwareConfigV1Create,
Read: resourceSoftwareConfigV1Read,
Delete: resourceSoftwareConfigV1Delete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(10 * time.Minute),
Delete: schema.DefaultTimeout(3 * time.Minute),
},
Schema: map[string]*schema.Schema{ //request and response parameters
"region": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Computed: true,
},
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"config": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"group": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"options": {
Type: schema.TypeMap,
Optional: true,
ForceNew: true,
},
"input_values": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeMap},
},
"output_values": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeMap},
},
},
}
}
func resourceOptionsV1(d *schema.ResourceData) map[string]interface{} {
m := make(map[string]interface{})
for key, val := range d.Get("options").(map[string]interface{}) {
m[key] = val.(string)
}
return m
}
func resourceSoftwareConfigV1Create(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
orchastrationClient, err := config.orchestrationV1Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating OpenTelekomCloud RTS client: %s", err)
}
input := d.Get("input_values").([]interface{})
inputs := make([]map[string]interface{}, len(input))
for i, v := range input {
inputs[i] = v.(map[string]interface{})
}
output := d.Get("output_values").([]interface{})
outputs := make([]map[string]interface{}, len(output))
for i, v := range output {
outputs[i] = v.(map[string]interface{})
}
createOpts := softwareconfig.CreateOpts{
Name: d.Get("name").(string),
Config: d.Get("config").(string),
Group: d.Get("group").(string),
Inputs: inputs,
Outputs: outputs,
Options: resourceOptionsV1(d),
}
n, err := softwareconfig.Create(orchastrationClient, createOpts).Extract()
if err != nil {
return fmt.Errorf("Error creating OpenTelekomCloud RTS Software Config: %s", err)
}
d.SetId(n.Id)
return resourceSoftwareConfigV1Read(d, meta)
}
func resourceSoftwareConfigV1Read(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
orchastrationClient, err := config.orchestrationV1Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating OpenTelekomCloud RTS client: %s", err)
}
n, err := softwareconfig.Get(orchastrationClient, d.Id()).Extract()
if err != nil {
if _, ok := err.(golangsdk.ErrDefault404); ok {
d.SetId("")
return nil
}
return fmt.Errorf("Error retrieving OpenTelekomCloud Vpc: %s", err)
}
d.Set("id", n.Id)
d.Set("name", n.Name)
d.Set("config", n.Config)
d.Set("group", n.Group)
d.Set("options", n.Options)
d.Set("region", GetRegion(d, config))
if err := d.Set("input_values", n.Inputs); err != nil {
return fmt.Errorf("[DEBUG] Error saving inputs to state for OpenTelekomCloud RTS Software Config (%s): %s", d.Id(), err)
}
if err := d.Set("output_values", n.Outputs); err != nil {
return fmt.Errorf("[DEBUG] Error saving outputs to state for OpenTelekomCloud RTS Software Config (%s): %s", d.Id(), err)
}
return nil
}
func resourceSoftwareConfigV1Delete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
orchastrationClient, err := config.orchestrationV1Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating OpenTelekomCloud vpc: %s", err)
}
err = softwareconfig.Delete(orchastrationClient, d.Id()).ExtractErr()
if err != nil {
if _, ok := err.(golangsdk.ErrDefault404); ok {
log.Printf("[INFO] Successfully deleted OpenTelekomCloud RTS Software Config %s", d.Id())
}
if errCode, ok := err.(golangsdk.ErrUnexpectedResponseCode); ok {
if errCode.Actual == 409 {
log.Printf("[INFO] Error deleting OpenTelekomCloud RTS Software Config %s", d.Id())
}
}
log.Printf("[INFO] Successfully deleted OpenTelekomCloud RTS Software Config %s", d.Id())
}
d.SetId("")
return nil
}