Skip to content

Commit

Permalink
cloudstack configuration resource
Browse files Browse the repository at this point in the history
  • Loading branch information
poddm committed Jun 26, 2024
1 parent 05dc88b commit afb0d11
Show file tree
Hide file tree
Showing 3 changed files with 329 additions and 0 deletions.
1 change: 1 addition & 0 deletions cloudstack/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ func New() *schema.Provider {
"cloudstack_affinity_group": resourceCloudStackAffinityGroup(),
"cloudstack_attach_volume": resourceCloudStackAttachVolume(),
"cloudstack_autoscale_vm_profile": resourceCloudStackAutoScaleVMProfile(),
"cloudstack_configuration": resourceCloudStackConfiguration(),
"cloudstack_disk": resourceCloudStackDisk(),
"cloudstack_egress_firewall": resourceCloudStackEgressFirewall(),
"cloudstack_firewall": resourceCloudStackFirewall(),
Expand Down
227 changes: 227 additions & 0 deletions cloudstack/resource_cloudstack_configuration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,227 @@
//
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
//

package cloudstack

import (
"fmt"

"github.com/apache/cloudstack-go/v2/cloudstack"
"github.com/hashicorp/terraform/helper/schema"
)

func resourceCloudStackConfiguration() *schema.Resource {
return &schema.Resource{
Create: resourceCloudStackConfigurationCreate,
Read: resourceCloudStackConfigurationRead,
Update: resourceCloudStackConfigurationUpdate,
Delete: resourceCloudStackConfigurationDelete,
Importer: &schema.ResourceImporter{
State: importStatePassthrough,
},

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"accountid": {
Type: schema.TypeString,
Optional: true,
},
"clusterid": {
Type: schema.TypeString,
Optional: true,
},
"domainid": {
Type: schema.TypeString,
Optional: true,
},
"imagestoreuuid": {
Type: schema.TypeString,
Optional: true,
},
"storeid": {
Type: schema.TypeString,
Optional: true,
},
"value": {
Type: schema.TypeString,
Optional: true,
},
"zoneid": {
Type: schema.TypeString,
Optional: true,
},
// computed
"category": {
Type: schema.TypeString,
Computed: true,
},
"description": {
Type: schema.TypeString,
Computed: true,
},
"is_dynamic": {
Type: schema.TypeString,
Computed: true,
},
"scope": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

func resourceCloudStackConfigurationRead(d *schema.ResourceData, meta interface{}) error {
cs := meta.(*cloudstack.CloudStackClient)
p := cs.Configuration.NewListConfigurationsParams()

// required
p.SetName(d.Id())

// optional
if v, ok := d.GetOk("accountid"); ok {
p.SetAccountid(v.(string))
}
if v, ok := d.GetOk("category"); ok {
p.SetCategory(v.(string))
}
if v, ok := d.GetOk("clusterid"); ok {
p.SetClusterid(v.(string))
}
if v, ok := d.GetOk("domainid"); ok {
p.SetDomainid(v.(string))
}
if v, ok := d.GetOk("imagestoreuuid"); ok {
p.SetImagestoreuuid(v.(string))
}
if v, ok := d.GetOk("storeid"); ok {
p.SetStorageid(v.(string))
}
if v, ok := d.GetOk("zoneid"); ok {
p.SetZoneid(v.(string))
}

cfg, err := cs.Configuration.ListConfigurations(p)
if err != nil {
return err
}

found := false
for _, v := range cfg.Configurations {
if v.Name == d.Id() {
d.Set("category", v.Category)
d.Set("description", v.Description)
d.Set("is_dynamic", v.Isdynamic)
d.Set("name", v.Name)
d.Set("value", v.Value)
d.Set("scope", v.Scope)
found = true
}
}

if !found {
return fmt.Errorf("listConfiguration failed. no matching names found %s", d.Id())
}

return nil

}

func resourceCloudStackConfigurationCreate(d *schema.ResourceData, meta interface{}) error {
if v, ok := d.GetOk("name"); ok {
d.SetId(v.(string))
}

resourceCloudStackConfigurationUpdate(d, meta)

return nil

}

func resourceCloudStackConfigurationUpdate(d *schema.ResourceData, meta interface{}) error {
cs := meta.(*cloudstack.CloudStackClient)
p := cs.Configuration.NewUpdateConfigurationParams(d.Id())

// Optional
if v, ok := d.GetOk("accountid"); ok {
p.SetAccountid(v.(string))
}
if v, ok := d.GetOk("clusterid"); ok {
p.SetClusterid(v.(string))
}
if v, ok := d.GetOk("domainid"); ok {
p.SetDomainid(v.(string))
}
if v, ok := d.GetOk("imagestoreuuid"); ok {
p.SetImagestoreuuid(v.(string))
}
if v, ok := d.GetOk("storeid"); ok {
p.SetStorageid(v.(string))
}
if v, ok := d.GetOk("value"); ok {
p.SetValue(v.(string))
}
if v, ok := d.GetOk("zoneid"); ok {
p.SetZoneid(v.(string))
}

_, err := cs.Configuration.UpdateConfiguration(p)
if err != nil {
return err
}

resourceCloudStackConfigurationRead(d, meta)

return nil
}

func resourceCloudStackConfigurationDelete(d *schema.ResourceData, meta interface{}) error {
cs := meta.(*cloudstack.CloudStackClient)
p := cs.Configuration.NewResetConfigurationParams(d.Id())

// Optional
if v, ok := d.GetOk("accountid"); ok {
p.SetAccountid(v.(string))
}
if v, ok := d.GetOk("clusterid"); ok {
p.SetClusterid(v.(string))
}
if v, ok := d.GetOk("domainid"); ok {
p.SetDomainid(v.(string))
}
if v, ok := d.GetOk("imagestoreuuid"); ok {
p.SetImagestoreid(v.(string))
}
if v, ok := d.GetOk("storeid"); ok {
p.SetStorageid(v.(string))
}
if v, ok := d.GetOk("zoneid"); ok {
p.SetZoneid(v.(string))
}

_, err := cs.Configuration.ResetConfiguration(p)
if err != nil {
return err
}

return nil
}
101 changes: 101 additions & 0 deletions cloudstack/resource_cloudstack_configuration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package cloudstack

import (
"fmt"
"testing"

"github.com/apache/cloudstack-go/v2/cloudstack"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)

func TestAccCloudStackConfiguration_basic(t *testing.T) {
var configuration cloudstack.ListConfigurationsResponse

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckCloudStackConfigurationDestroy,
Steps: []resource.TestStep{
{
Config: testAccResourceConfiguration(),
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudStackConfigurationExists("cloudstack_configuration.test", &configuration),
resource.TestCheckResourceAttr("cloudstack_configuration.test", "value", "test_host"),
),
},
},
})
}

func TestAccCloudStackConfiguration_import(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccResourceConfiguration(),
},

{
ResourceName: "cloudstack_configuration.test",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccCheckCloudStackConfigurationExists(n string, configuration *cloudstack.ListConfigurationsResponse) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
}

if rs.Primary.ID == "" {
return fmt.Errorf("configuration ID not set")
}

cs := testAccProvider.Meta().(*cloudstack.CloudStackClient)
p := cs.Configuration.NewListConfigurationsParams()
p.SetName(rs.Primary.ID)

cfg, err := cs.Configuration.ListConfigurations(p)
if err != nil {
return err
}

*configuration = *cfg

return nil
}
}

func testAccCheckCloudStackConfigurationAttributes(configuration *cloudstack.ListConfigurationsResponse) resource.TestCheckFunc {
return func(s *terraform.State) error {
for _, v := range configuration.Configurations {
if v.Name == "host" {
if v.Value != "test_host" {
return fmt.Errorf("Bad value: %s", v.Value)
}
return nil
}
}
return fmt.Errorf("Bad name: %s", "host")
}
}

func testAccCheckCloudStackConfigurationDestroy(s *terraform.State) error {
return nil

}

func testAccResourceConfiguration() string {
return fmt.Sprintf(`
resource "cloudstack_configuration" "test" {
name = "host"
value = "test_host"
}
`)
}

0 comments on commit afb0d11

Please sign in to comment.