Skip to content

Commit 738f8bf

Browse files
committed
Adding Configuration resource
1 parent 61f86a9 commit 738f8bf

File tree

7 files changed

+522
-9
lines changed

7 files changed

+522
-9
lines changed

cloudstack/provider.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,12 @@ func Provider() terraform.ResourceProvider {
8484
ResourcesMap: map[string]*schema.Resource{
8585
"cloudstack_affinity_group": resourceCloudStackAffinityGroup(),
8686
"cloudstack_autoscale_vm_profile": resourceCloudStackAutoScaleVMProfile(),
87+
"cloudstack_configuration": resourceCloudStackConfiguration(),
8788
"cloudstack_disk": resourceCloudStackDisk(),
8889
"cloudstack_egress_firewall": resourceCloudStackEgressFirewall(),
8990
"cloudstack_firewall": resourceCloudStackFirewall(),
9091
"cloudstack_instance": resourceCloudStackInstance(),
9192
"cloudstack_ipaddress": resourceCloudStackIPAddress(),
92-
"cloudstack_kubernetes_cluster": resourceCloudStackKubernetesCluster(),
93-
"cloudstack_kubernetes_version": resourceCloudStackKubernetesVersion(),
9493
"cloudstack_loadbalancer_rule": resourceCloudStackLoadBalancerRule(),
9594
"cloudstack_network": resourceCloudStackNetwork(),
9695
"cloudstack_network_acl": resourceCloudStackNetworkACL(),
Lines changed: 227 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,227 @@
1+
//
2+
// Licensed to the Apache Software Foundation (ASF) under one
3+
// or more contributor license agreements. See the NOTICE file
4+
// distributed with this work for additional information
5+
// regarding copyright ownership. The ASF licenses this file
6+
// to you under the Apache License, Version 2.0 (the
7+
// "License"); you may not use this file except in compliance
8+
// with the License. You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing,
13+
// software distributed under the License is distributed on an
14+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
// KIND, either express or implied. See the License for the
16+
// specific language governing permissions and limitations
17+
// under the License.
18+
//
19+
20+
package cloudstack
21+
22+
import (
23+
"fmt"
24+
25+
"github.com/apache/cloudstack-go/v2/cloudstack"
26+
"github.com/hashicorp/terraform/helper/schema"
27+
)
28+
29+
func resourceCloudStackConfiguration() *schema.Resource {
30+
return &schema.Resource{
31+
Create: resourceCloudStackConfigurationCreate,
32+
Read: resourceCloudStackConfigurationRead,
33+
Update: resourceCloudStackConfigurationUpdate,
34+
Delete: resourceCloudStackConfigurationDelete,
35+
Importer: &schema.ResourceImporter{
36+
State: importStatePassthrough,
37+
},
38+
39+
Schema: map[string]*schema.Schema{
40+
"name": {
41+
Type: schema.TypeString,
42+
Required: true,
43+
},
44+
"accountid": {
45+
Type: schema.TypeString,
46+
Optional: true,
47+
},
48+
"clusterid": {
49+
Type: schema.TypeString,
50+
Optional: true,
51+
},
52+
"domainid": {
53+
Type: schema.TypeString,
54+
Optional: true,
55+
},
56+
"imagestoreuuid": {
57+
Type: schema.TypeString,
58+
Optional: true,
59+
},
60+
"storeid": {
61+
Type: schema.TypeString,
62+
Optional: true,
63+
},
64+
"value": {
65+
Type: schema.TypeString,
66+
Optional: true,
67+
},
68+
"zoneid": {
69+
Type: schema.TypeString,
70+
Optional: true,
71+
},
72+
// computed
73+
"category": {
74+
Type: schema.TypeString,
75+
Computed: true,
76+
},
77+
"description": {
78+
Type: schema.TypeString,
79+
Computed: true,
80+
},
81+
"is_dynamic": {
82+
Type: schema.TypeString,
83+
Computed: true,
84+
},
85+
"scope": {
86+
Type: schema.TypeString,
87+
Computed: true,
88+
},
89+
},
90+
}
91+
}
92+
93+
func resourceCloudStackConfigurationRead(d *schema.ResourceData, meta interface{}) error {
94+
cs := meta.(*cloudstack.CloudStackClient)
95+
p := cs.Configuration.NewListConfigurationsParams()
96+
97+
// required
98+
p.SetName(d.Id())
99+
100+
// optional
101+
if v, ok := d.GetOk("accountid"); ok {
102+
p.SetAccountid(v.(string))
103+
}
104+
if v, ok := d.GetOk("category"); ok {
105+
p.SetCategory(v.(string))
106+
}
107+
if v, ok := d.GetOk("clusterid"); ok {
108+
p.SetClusterid(v.(string))
109+
}
110+
if v, ok := d.GetOk("domainid"); ok {
111+
p.SetDomainid(v.(string))
112+
}
113+
if v, ok := d.GetOk("imagestoreuuid"); ok {
114+
p.SetImagestoreuuid(v.(string))
115+
}
116+
if v, ok := d.GetOk("storeid"); ok {
117+
p.SetStorageid(v.(string))
118+
}
119+
if v, ok := d.GetOk("zoneid"); ok {
120+
p.SetZoneid(v.(string))
121+
}
122+
123+
cfg, err := cs.Configuration.ListConfigurations(p)
124+
if err != nil {
125+
return err
126+
}
127+
128+
found := false
129+
for _, v := range cfg.Configurations {
130+
if v.Name == d.Id() {
131+
d.Set("category", v.Category)
132+
d.Set("description", v.Description)
133+
d.Set("is_dynamic", v.Isdynamic)
134+
d.Set("name", v.Name)
135+
d.Set("value", v.Value)
136+
d.Set("scope", v.Scope)
137+
found = true
138+
}
139+
}
140+
141+
if !found {
142+
return fmt.Errorf("listConfiguration failed. no matching names found %s", d.Id())
143+
}
144+
145+
return nil
146+
147+
}
148+
149+
func resourceCloudStackConfigurationCreate(d *schema.ResourceData, meta interface{}) error {
150+
if v, ok := d.GetOk("name"); ok {
151+
d.SetId(v.(string))
152+
}
153+
154+
resourceCloudStackConfigurationUpdate(d, meta)
155+
156+
return nil
157+
158+
}
159+
160+
func resourceCloudStackConfigurationUpdate(d *schema.ResourceData, meta interface{}) error {
161+
cs := meta.(*cloudstack.CloudStackClient)
162+
p := cs.Configuration.NewUpdateConfigurationParams(d.Id())
163+
164+
// Optional
165+
if v, ok := d.GetOk("accountid"); ok {
166+
p.SetAccountid(v.(string))
167+
}
168+
if v, ok := d.GetOk("clusterid"); ok {
169+
p.SetClusterid(v.(string))
170+
}
171+
if v, ok := d.GetOk("domainid"); ok {
172+
p.SetDomainid(v.(string))
173+
}
174+
if v, ok := d.GetOk("imagestoreuuid"); ok {
175+
p.SetImagestoreuuid(v.(string))
176+
}
177+
if v, ok := d.GetOk("storeid"); ok {
178+
p.SetStorageid(v.(string))
179+
}
180+
if v, ok := d.GetOk("value"); ok {
181+
p.SetValue(v.(string))
182+
}
183+
if v, ok := d.GetOk("zoneid"); ok {
184+
p.SetZoneid(v.(string))
185+
}
186+
187+
_, err := cs.Configuration.UpdateConfiguration(p)
188+
if err != nil {
189+
return err
190+
}
191+
192+
resourceCloudStackConfigurationRead(d, meta)
193+
194+
return nil
195+
}
196+
197+
func resourceCloudStackConfigurationDelete(d *schema.ResourceData, meta interface{}) error {
198+
cs := meta.(*cloudstack.CloudStackClient)
199+
p := cs.Configuration.NewResetConfigurationParams(d.Id())
200+
201+
// Optional
202+
if v, ok := d.GetOk("accountid"); ok {
203+
p.SetAccountid(v.(string))
204+
}
205+
if v, ok := d.GetOk("clusterid"); ok {
206+
p.SetClusterid(v.(string))
207+
}
208+
if v, ok := d.GetOk("domainid"); ok {
209+
p.SetDomainid(v.(string))
210+
}
211+
if v, ok := d.GetOk("imagestoreuuid"); ok {
212+
p.SetImagestoreid(v.(string))
213+
}
214+
if v, ok := d.GetOk("storeid"); ok {
215+
p.SetStorageid(v.(string))
216+
}
217+
if v, ok := d.GetOk("zoneid"); ok {
218+
p.SetZoneid(v.(string))
219+
}
220+
221+
_, err := cs.Configuration.ResetConfiguration(p)
222+
if err != nil {
223+
return err
224+
}
225+
226+
return nil
227+
}
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
package cloudstack
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/apache/cloudstack-go/v2/cloudstack"
8+
"github.com/hashicorp/terraform/helper/resource"
9+
"github.com/hashicorp/terraform/terraform"
10+
)
11+
12+
func TestAccCloudStackConfiguration_basic(t *testing.T) {
13+
var configuration cloudstack.ListConfigurationsResponse
14+
15+
resource.Test(t, resource.TestCase{
16+
PreCheck: func() { testAccPreCheck(t) },
17+
Providers: testAccProviders,
18+
Steps: []resource.TestStep{
19+
{
20+
Config: testAccResourceConfiguration(),
21+
Check: resource.ComposeTestCheckFunc(
22+
testAccCheckCloudStackConfigurationExists("cloudstack_configuration.test", &configuration),
23+
testAccCheckCloudStackConfigurationAttributes(&configuration),
24+
),
25+
},
26+
},
27+
})
28+
}
29+
30+
func TestAccCloudStackConfiguration_update(t *testing.T) {
31+
var configuration cloudstack.ListConfigurationsResponse
32+
// var configuration_update cloudstack.UpdateConfigurationResponse
33+
34+
resource.Test(t, resource.TestCase{
35+
PreCheck: func() { testAccPreCheck(t) },
36+
Providers: testAccProviders,
37+
Steps: []resource.TestStep{
38+
{
39+
Config: testAccResourceConfiguration(),
40+
Check: resource.ComposeTestCheckFunc(
41+
testAccCheckCloudStackConfigurationExists("cloudstack_configuration.test", &configuration),
42+
testAccCheckCloudStackConfigurationAttributes(&configuration),
43+
resource.TestCheckResourceAttr("cloudstack_configuration.test", "value", "test_host"),
44+
),
45+
},
46+
47+
{
48+
Config: testAccResourceConfiguration_update(),
49+
Check: resource.ComposeTestCheckFunc(
50+
testAccCheckCloudStackConfigurationExists("cloudstack_configuration.test", &configuration),
51+
// testAccCheckCloudStackConfigurationUpdate(&configuration),
52+
resource.TestCheckResourceAttr("cloudstack_configuration.test", "value", "new_test_host"),
53+
),
54+
},
55+
},
56+
})
57+
}
58+
59+
func TestAccCloudStackConfiguration_import(t *testing.T) {
60+
resource.Test(t, resource.TestCase{
61+
PreCheck: func() { testAccPreCheck(t) },
62+
Providers: testAccProviders,
63+
Steps: []resource.TestStep{
64+
{
65+
Config: testAccResourceConfiguration(),
66+
},
67+
68+
{
69+
ResourceName: "cloudstack_configuration.test",
70+
ImportState: true,
71+
ImportStateVerify: true,
72+
},
73+
},
74+
})
75+
}
76+
77+
func testAccCheckCloudStackConfigurationExists(n string, configuration *cloudstack.ListConfigurationsResponse) resource.TestCheckFunc {
78+
return func(s *terraform.State) error {
79+
rs, ok := s.RootModule().Resources[n]
80+
if !ok {
81+
return fmt.Errorf("Not found: %s", n)
82+
}
83+
84+
if rs.Primary.ID == "" {
85+
return fmt.Errorf("configuration ID not set")
86+
}
87+
88+
cs := testAccProvider.Meta().(*cloudstack.CloudStackClient)
89+
p := cs.Configuration.NewListConfigurationsParams()
90+
p.SetName(rs.Primary.ID)
91+
92+
cfg, err := cs.Configuration.ListConfigurations(p)
93+
if err != nil {
94+
return err
95+
}
96+
97+
*configuration = *cfg
98+
99+
return nil
100+
}
101+
}
102+
103+
func testAccCheckCloudStackConfigurationAttributes(configuration *cloudstack.ListConfigurationsResponse) resource.TestCheckFunc {
104+
return func(s *terraform.State) error {
105+
106+
if configuration.Configurations[0].Name != "host" {
107+
return fmt.Errorf("Bad name: %s", configuration.Configurations[0].Name)
108+
}
109+
110+
if configuration.Configurations[0].Value != "test_host" {
111+
return fmt.Errorf("Bad name: %s", configuration.Configurations[0].Name)
112+
}
113+
114+
return nil
115+
}
116+
}
117+
118+
func testAccCheckCloudStackConfigurationUpdate(configuration *cloudstack.ListConfigurationsResponse) resource.TestCheckFunc {
119+
return func(s *terraform.State) error {
120+
if configuration.Configurations[0].Name != "host" {
121+
return fmt.Errorf("Bad name: %s", configuration.Configurations[0].Name)
122+
}
123+
124+
if configuration.Configurations[0].Value != "new_test_host" {
125+
return fmt.Errorf("Bad name: %s", configuration.Configurations[0].Name)
126+
}
127+
128+
return nil
129+
}
130+
}
131+
132+
func testAccResourceConfiguration() string {
133+
return fmt.Sprintf(`
134+
resource "cloudstack_configuration" "test" {
135+
name = "host"
136+
value = "test_host"
137+
}
138+
`)
139+
}
140+
141+
func testAccResourceConfiguration_update() string {
142+
return fmt.Sprintf(`
143+
resource "cloudstack_configuration" "test" {
144+
name = "host"
145+
value = "new_test_host"
146+
}
147+
`)
148+
}

0 commit comments

Comments
 (0)