Skip to content

Adding configuration resource #57

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cloudstack/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ func Provider() *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
239 changes: 239 additions & 0 deletions cloudstack/resource_cloudstack_configuration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,239 @@
//
// 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-plugin-sdk/v2/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": {
Description: "configuration by name",
Type: schema.TypeString,
Required: true,
},
"account_id": {
Description: "the ID of the Account to update the parameter value for corresponding account",
Type: schema.TypeString,
Optional: true,
},
"cluster_id": {
Description: "the ID of the Cluster to update the parameter value for corresponding cluster",
Type: schema.TypeString,
Optional: true,
},
"domain_id": {
Description: "the ID of the Domain to update the parameter value for corresponding domain",
Type: schema.TypeString,
Optional: true,
},
"image_store_uuid": {
Description: "the ID of the Image Store to update the parameter value for corresponding image store",
Type: schema.TypeString,
Optional: true,
},
"store_id": {
Description: "the ID of the Storage pool to update the parameter value for corresponding storage pool",
Type: schema.TypeString,
Optional: true,
},
"value": {
Description: "the value of the configuration",
Type: schema.TypeString,
Optional: true,
},
"zone_id": {
Description: "the ID of the Zone to update the parameter value for corresponding zone",
Type: schema.TypeString,
Optional: true,
},
// computed
"category": {
Description: "configurations by category",
Type: schema.TypeString,
Computed: true,
},
"description": {
Description: "the description of the configuration",
Type: schema.TypeString,
Computed: true,
},
"is_dynamic": {
Description: "true if the configuration is dynamic",
Type: schema.TypeBool,
Computed: true,
},
"scope": {
Description: "scope(zone/cluster/pool/account) of the parameter that needs to be updated",
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("account_id"); ok {
p.SetAccountid(v.(string))
}
if v, ok := d.GetOk("category"); ok {
p.SetCategory(v.(string))
}
if v, ok := d.GetOk("cluster_id"); ok {
p.SetClusterid(v.(string))
}
if v, ok := d.GetOk("domain_id"); ok {
p.SetDomainid(v.(string))
}
if v, ok := d.GetOk("image_store_uuid"); ok {
p.SetImagestoreuuid(v.(string))
}
if v, ok := d.GetOk("store_id"); ok {
p.SetStorageid(v.(string))
}
if v, ok := d.GetOk("zone_id"); 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("account_id"); ok {
p.SetAccountid(v.(string))
}
if v, ok := d.GetOk("cluster_id"); ok {
p.SetClusterid(v.(string))
}
if v, ok := d.GetOk("domain_id"); ok {
p.SetDomainid(v.(string))
}
if v, ok := d.GetOk("image_store_uuid"); ok {
p.SetImagestoreuuid(v.(string))
}
if v, ok := d.GetOk("store_id"); ok {
p.SetStorageid(v.(string))
}
if v, ok := d.GetOk("value"); ok {
p.SetValue(v.(string))
}
if v, ok := d.GetOk("zone_id"); 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("account_id"); ok {
p.SetAccountid(v.(string))
}
if v, ok := d.GetOk("cluster_id"); ok {
p.SetClusterid(v.(string))
}
if v, ok := d.GetOk("domain_id"); ok {
p.SetDomainid(v.(string))
}
if v, ok := d.GetOk("image_store_uuid"); ok {
p.SetImagestoreid(v.(string))
}
if v, ok := d.GetOk("store_id"); ok {
p.SetStorageid(v.(string))
}
if v, ok := d.GetOk("zone_id"); 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-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/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"
}
`)
}
Loading