Skip to content
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
3 changes: 3 additions & 0 deletions cloudstack/resource_cloudstack_autoscale_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ func resourceCloudStackAutoScalePolicy() *schema.Resource {
Read: resourceCloudStackAutoScalePolicyRead,
Update: resourceCloudStackAutoScalePolicyUpdate,
Delete: resourceCloudStackAutoScalePolicyDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"name": {
Expand Down
3 changes: 3 additions & 0 deletions cloudstack/resource_cloudstack_autoscale_vm_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ func resourceCloudStackAutoScaleVMGroup() *schema.Resource {
Read: resourceCloudStackAutoScaleVMGroupRead,
Update: resourceCloudStackAutoScaleVMGroupUpdate,
Delete: resourceCloudStackAutoScaleVMGroupDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"lbrule_id": {
Expand Down
3 changes: 3 additions & 0 deletions cloudstack/resource_cloudstack_autoscale_vm_profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ func resourceCloudStackAutoScaleVMProfile() *schema.Resource {
Read: resourceCloudStackAutoScaleVMProfileRead,
Update: resourceCloudStackAutoScaleVMProfileUpdate,
Delete: resourceCloudStackAutoScaleVMProfileDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"service_offering": {
Expand Down
21 changes: 21 additions & 0 deletions cloudstack/resource_cloudstack_autoscale_vm_profile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,27 @@ func TestAccCloudStackAutoscaleVMProfile_update(t *testing.T) {
})
}

func TestAccCloudStackAutoscaleVMProfile_import(t *testing.T) {
t.Skip("Skipping due to bug in cloudstack-go library")

Comment on lines +88 to +90
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckCloudStackAutoscaleVMProfileDestroy,
Steps: []resource.TestStep{
{
Config: testAccCloudStackAutoscaleVMProfile_basic,
},

{
ResourceName: "cloudstack_autoscale_vm_profile.foo",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccCheckResourceMetadata(vmProfile *cloudstack.AutoScaleVmProfile) resource.TestCheckFunc {
return func(s *terraform.State) error {
cs := testAccProvider.Meta().(*cloudstack.CloudStackClient)
Expand Down
3 changes: 3 additions & 0 deletions cloudstack/resource_cloudstack_condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ func resourceCloudStackCondition() *schema.Resource {
Read: resourceCloudStackConditionRead,
Update: resourceCloudStackConditionUpdate,
Delete: resourceCloudStackConditionDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"counter_id": {
Expand Down
3 changes: 3 additions & 0 deletions cloudstack/resource_cloudstack_counter.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ func resourceCloudStackCounter() *schema.Resource {
Create: resourceCloudStackCounterCreate,
Read: resourceCloudStackCounterRead,
Delete: resourceCloudStackCounterDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"name": {
Expand Down
22 changes: 16 additions & 6 deletions cloudstack/resource_cloudstack_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ func resourceCloudStackHost() *schema.Resource {
Update: resourceCloudStackHostUpdate,
Create: resourceCloudStackHostCreate,
Delete: resourceCloudStackHostDelete,
// NOTE: CloudStack's listHosts API never returns url, username, or
// password, so Read cannot repopulate them after import. url is also
// ForceNew, so leaving it unset in an imported config will plan a
// destroy/recreate of the host. After importing, set these fields
// explicitly in config (or use lifecycle.ignore_changes) to avoid
// unexpected replacement on the next apply.
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Comment on lines +46 to +48
Schema: map[string]*schema.Schema{
"hypervisor": {
Type: schema.TypeString,
Expand Down Expand Up @@ -224,12 +233,13 @@ func resourceCloudStackHostRead(d *schema.ResourceData, meta interface{}) error
d.SetId(h.Id)

fields := map[string]interface{}{
"hypervisor": h.Hypervisor,
"pod_id": h.Podid,
"zone_id": h.Zoneid,
"state": h.State,
"resource_state": h.Resourcestate,
"name": h.Name,
"hypervisor": h.Hypervisor,
"pod_id": h.Podid,
"zone_id": h.Zoneid,
"state": h.State,
"resource_state": h.Resourcestate,
"allocation_state": h.Resourcestate,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"resource_state" and "allocation_state" both are set as h.Resourcestate

confirm if this is correct?

"name": h.Name,
}

for k, v := range fields {
Expand Down
26 changes: 26 additions & 0 deletions cloudstack/resource_cloudstack_host_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,32 @@ func TestAccCloudStackHost_basic(t *testing.T) {
})
}

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

{
ResourceName: "cloudstack_host.test",
ImportState: true,
ImportStateVerify: true,
// CloudStack's listHosts API never returns the connection URL or
// credentials, and these timeouts/flags are provider-local knobs that
// aren't part of the host object, so Read can't populate any of them.
ImportStateVerifyIgnore: []string{
"url", "username", "password",
"create_timeout", "destroy_timeout",
"prevent_destroy", "force_destroy",
},
},
},
})
}

const testAccCloudStackHost_basic = `
data "cloudstack_zone" "zone" {
filter {
Expand Down
44 changes: 44 additions & 0 deletions cloudstack/resource_cloudstack_ipaddress.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ func resourceCloudStackIPAddress() *schema.Resource {
Create: resourceCloudStackIPAddressCreate,
Read: resourceCloudStackIPAddressRead,
Delete: resourceCloudStackIPAddressDelete,
Importer: &schema.ResourceImporter{
State: resourceCloudStackIPAddressImport,
},

Schema: map[string]*schema.Schema{
"is_portable": {
Expand Down Expand Up @@ -171,6 +174,41 @@ func resourceCloudStackIPAddressCreate(d *schema.ResourceData, meta interface{})
return resourceCloudStackIPAddressRead(d, meta)
}

func resourceCloudStackIPAddressImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
cs := meta.(*cloudstack.CloudStackClient)

// Try to split the ID to extract the optional project name.
s := strings.SplitN(d.Id(), "/", 2)
if len(s) == 2 {
d.Set("project", s[0])
}

ipAddressID := s[len(s)-1]
d.SetId(ipAddressID)

ip, count, err := cs.Address.GetPublicIpAddressByID(
ipAddressID,
cloudstack.WithProject(d.Get("project").(string)),
)
if err != nil {
if count == 0 {
return nil, fmt.Errorf("IP address with ID %s does not exist", ipAddressID)
}
return nil, err
}

// Seed whichever of network_id/vpc_id actually applies before Read runs:
// Read only refreshes these when already present in state, so import
// needs to set the right one here first.
if ip.Vpcid != "" {
d.Set("vpc_id", ip.Vpcid)
} else if ip.Associatednetworkid != "" {
d.Set("network_id", ip.Associatednetworkid)
}

return []*schema.ResourceData{d}, nil
}

func resourceCloudStackIPAddressRead(d *schema.ResourceData, meta interface{}) error {
cs := meta.(*cloudstack.CloudStackClient)

Expand Down Expand Up @@ -208,6 +246,12 @@ func resourceCloudStackIPAddressRead(d *schema.ResourceData, meta interface{}) e
// Updated the IP address
d.Set("ip_address", ip.Ipaddress)

// Only refresh network_id/vpc_id if already present in state: a plain
// zone-scoped IP (neither set) can still come back from the API with an
// associated network under the hood, and syncing that into an Optional,
// non-Computed, ForceNew field would create a permanent diff. The
// importer is responsible for seeding whichever one applies before this
// Read runs.
if _, ok := d.GetOk("network_id"); ok {
d.Set("network_id", ip.Associatednetworkid)
}
Expand Down
19 changes: 19 additions & 0 deletions cloudstack/resource_cloudstack_ipaddress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,25 @@ func TestAccCloudStackIPAddress_basic(t *testing.T) {
})
}

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

{
ResourceName: "cloudstack_ipaddress.foo",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccCloudStackIPAddress_vpc(t *testing.T) {
var ipaddr cloudstack.PublicIpAddress

Expand Down
3 changes: 3 additions & 0 deletions cloudstack/resource_cloudstack_loadbalancer_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ func resourceCloudStackLoadBalancerRule() *schema.Resource {
Read: resourceCloudStackLoadBalancerRuleRead,
Update: resourceCloudStackLoadBalancerRuleUpdate,
Delete: resourceCloudStackLoadBalancerRuleDelete,
Importer: &schema.ResourceImporter{
State: importStatePassthrough,
},

Schema: map[string]*schema.Schema{
"name": {
Expand Down
21 changes: 21 additions & 0 deletions cloudstack/resource_cloudstack_loadbalancer_rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,27 @@ func TestAccCloudStackLoadBalancerRule_basic(t *testing.T) {
})
}

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

{
ResourceName: "cloudstack_loadbalancer_rule.foo",
ImportState: true,
ImportStateVerify: true,
// certificate_id can't be read back from the CloudStack API (write-only field)
ImportStateVerifyIgnore: []string{"certificate_id"},
},
},
})
}

func TestAccCloudStackLoadBalancerRule_update(t *testing.T) {
var id string

Expand Down
20 changes: 20 additions & 0 deletions cloudstack/resource_cloudstack_network_offering.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ func resourceCloudStackNetworkOffering() *schema.Resource {
Read: resourceCloudStackNetworkOfferingRead,
Update: resourceCloudStackNetworkOfferingUpdate,
Delete: resourceCloudStackNetworkOfferingDelete,
Importer: &schema.ResourceImporter{
State: resourceCloudStackNetworkOfferingImport,
},
Comment on lines +36 to +38
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Expand Down Expand Up @@ -342,6 +345,23 @@ func resourceCloudStackNetworkOfferingDelete(d *schema.ResourceData, meta interf
return nil
}

func resourceCloudStackNetworkOfferingImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
cs := meta.(*cloudstack.CloudStackClient)

// Read looks the offering up by name, so resolve the name from the ID first
n, count, err := cs.NetworkOffering.GetNetworkOfferingByID(d.Id())
if err != nil {
if count == 0 {
return nil, fmt.Errorf("network offering with ID %s does not exist", d.Id())
}
return nil, err
}

d.Set("name", n.Name)

return []*schema.ResourceData{d}, nil
}

func resourceCloudStackNetworkOfferingRead(d *schema.ResourceData, meta interface{}) error {
cs := meta.(*cloudstack.CloudStackClient)
log.Printf("[DEBUG] Retrieving Network Offering %s", d.Get("name").(string))
Expand Down
Loading
Loading