Skip to content

Commit a6c0a77

Browse files
author
Sander van Harmelen
authored
Merge pull request #56 from terraform-providers/svh/f-import-project
Add support to import resources when using projects
2 parents 5b65411 + 544cf84 commit a6c0a77

19 files changed

+150
-11
lines changed

cloudstack/resource_cloudstack_affinity_group.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func resourceCloudStackAffinityGroup() *schema.Resource {
1515
Read: resourceCloudStackAffinityGroupRead,
1616
Delete: resourceCloudStackAffinityGroupDelete,
1717
Importer: &schema.ResourceImporter{
18-
State: schema.ImportStatePassthrough,
18+
State: importStatePassthrough,
1919
},
2020

2121
Schema: map[string]*schema.Schema{

cloudstack/resource_cloudstack_disk.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func resourceCloudStackDisk() *schema.Resource {
1515
Update: resourceCloudStackDiskUpdate,
1616
Delete: resourceCloudStackDiskDelete,
1717
Importer: &schema.ResourceImporter{
18-
State: schema.ImportStatePassthrough,
18+
State: importStatePassthrough,
1919
},
2020

2121
Schema: map[string]*schema.Schema{

cloudstack/resource_cloudstack_instance.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,7 @@ func resourceCloudStackInstanceImport(d *schema.ResourceData, meta interface{})
620620
// We set start_vm to true as that matches the default and we assume that
621621
// when you need to import an instance it means it is already running.
622622
d.Set("start_vm", true)
623-
return []*schema.ResourceData{d}, nil
623+
return importStatePassthrough(d, meta)
624624
}
625625

626626
// getUserData returns the user data as a base64 encoded string

cloudstack/resource_cloudstack_instance_test.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,27 @@ func TestAccCloudStackInstance_import(t *testing.T) {
172172
})
173173
}
174174

175+
func TestAccCloudStackInstance_importProject(t *testing.T) {
176+
resource.Test(t, resource.TestCase{
177+
PreCheck: func() { testAccPreCheck(t) },
178+
Providers: testAccProviders,
179+
CheckDestroy: testAccCheckCloudStackInstanceDestroy,
180+
Steps: []resource.TestStep{
181+
{
182+
Config: testAccCloudStackInstance_project,
183+
},
184+
185+
{
186+
ResourceName: "cloudstack_instance.foobar",
187+
ImportState: true,
188+
ImportStateIdPrefix: "terraform/",
189+
ImportStateVerify: true,
190+
ImportStateVerifyIgnore: []string{"expunge", "user_data"},
191+
},
192+
},
193+
})
194+
}
195+
175196
func testAccCheckCloudStackInstanceExists(
176197
n string, instance *cloudstack.VirtualMachine) resource.TestCheckFunc {
177198
return func(s *terraform.State) error {
@@ -185,7 +206,10 @@ func testAccCheckCloudStackInstanceExists(
185206
}
186207

187208
cs := testAccProvider.Meta().(*cloudstack.CloudStackClient)
188-
vm, _, err := cs.VirtualMachine.GetVirtualMachineByID(rs.Primary.ID)
209+
vm, _, err := cs.VirtualMachine.GetVirtualMachineByID(
210+
rs.Primary.ID,
211+
cloudstack.WithProject(rs.Primary.Attributes["project"]),
212+
)
189213

190214
if err != nil {
191215
return err

cloudstack/resource_cloudstack_network.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func resourceCloudStackNetwork() *schema.Resource {
3838
Update: resourceCloudStackNetworkUpdate,
3939
Delete: resourceCloudStackNetworkDelete,
4040
Importer: &schema.ResourceImporter{
41-
State: schema.ImportStatePassthrough,
41+
State: importStatePassthrough,
4242
},
4343

4444
Schema: map[string]*schema.Schema{

cloudstack/resource_cloudstack_network_acl.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func resourceCloudStackNetworkACL() *schema.Resource {
1515
Read: resourceCloudStackNetworkACLRead,
1616
Delete: resourceCloudStackNetworkACLDelete,
1717
Importer: &schema.ResourceImporter{
18-
State: schema.ImportStatePassthrough,
18+
State: importStatePassthrough,
1919
},
2020

2121
Schema: map[string]*schema.Schema{

cloudstack/resource_cloudstack_network_test.go

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,27 @@ func TestAccCloudStackNetwork_basic(t *testing.T) {
3030
})
3131
}
3232

33+
func TestAccCloudStackNetwork_project(t *testing.T) {
34+
var network cloudstack.Network
35+
36+
resource.Test(t, resource.TestCase{
37+
PreCheck: func() { testAccPreCheck(t) },
38+
Providers: testAccProviders,
39+
CheckDestroy: testAccCheckCloudStackNetworkDestroy,
40+
Steps: []resource.TestStep{
41+
{
42+
Config: testAccCloudStackNetwork_project,
43+
Check: resource.ComposeTestCheckFunc(
44+
testAccCheckCloudStackNetworkExists(
45+
"cloudstack_network.foo", &network),
46+
resource.TestCheckResourceAttr(
47+
"cloudstack_network.foo", "project", "terraform"),
48+
),
49+
},
50+
},
51+
})
52+
}
53+
3354
func TestAccCloudStackNetwork_vpc(t *testing.T) {
3455
var network cloudstack.Network
3556

@@ -98,6 +119,26 @@ func TestAccCloudStackNetwork_import(t *testing.T) {
98119
})
99120
}
100121

122+
func TestAccCloudStackNetwork_importProject(t *testing.T) {
123+
resource.Test(t, resource.TestCase{
124+
PreCheck: func() { testAccPreCheck(t) },
125+
Providers: testAccProviders,
126+
CheckDestroy: testAccCheckCloudStackNetworkDestroy,
127+
Steps: []resource.TestStep{
128+
{
129+
Config: testAccCloudStackNetwork_project,
130+
},
131+
132+
{
133+
ResourceName: "cloudstack_network.foo",
134+
ImportState: true,
135+
ImportStateIdPrefix: "terraform/",
136+
ImportStateVerify: true,
137+
},
138+
},
139+
})
140+
}
141+
101142
func testAccCheckCloudStackNetworkExists(
102143
n string, network *cloudstack.Network) resource.TestCheckFunc {
103144
return func(s *terraform.State) error {
@@ -111,8 +152,10 @@ func testAccCheckCloudStackNetworkExists(
111152
}
112153

113154
cs := testAccProvider.Meta().(*cloudstack.CloudStackClient)
114-
ntwrk, _, err := cs.Network.GetNetworkByID(rs.Primary.ID)
115-
155+
ntwrk, _, err := cs.Network.GetNetworkByID(
156+
rs.Primary.ID,
157+
cloudstack.WithProject(rs.Primary.Attributes["project"]),
158+
)
116159
if err != nil {
117160
return err
118161
}
@@ -207,6 +250,15 @@ resource "cloudstack_network" "foo" {
207250
#}
208251
}`
209252

253+
const testAccCloudStackNetwork_project = `
254+
resource "cloudstack_network" "foo" {
255+
name = "terraform-network"
256+
cidr = "10.1.1.0/24"
257+
network_offering = "DefaultIsolatedNetworkOfferingWithSourceNatService"
258+
project = "terraform"
259+
zone = "Sandbox-simulator"
260+
}`
261+
210262
const testAccCloudStackNetwork_vpc = `
211263
resource "cloudstack_vpc" "foo" {
212264
name = "terraform-vpc"

cloudstack/resource_cloudstack_security_group.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func resourceCloudStackSecurityGroup() *schema.Resource {
1515
Read: resourceCloudStackSecurityGroupRead,
1616
Delete: resourceCloudStackSecurityGroupDelete,
1717
Importer: &schema.ResourceImporter{
18-
State: schema.ImportStatePassthrough,
18+
State: importStatePassthrough,
1919
},
2020

2121
Schema: map[string]*schema.Schema{

cloudstack/resource_cloudstack_vpc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func resourceCloudStackVPC() *schema.Resource {
1616
Update: resourceCloudStackVPCUpdate,
1717
Delete: resourceCloudStackVPCDelete,
1818
Importer: &schema.ResourceImporter{
19-
State: schema.ImportStatePassthrough,
19+
State: importStatePassthrough,
2020
},
2121

2222
Schema: map[string]*schema.Schema{

cloudstack/resource_cloudstack_vpn_customer_gateway.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func resourceCloudStackVPNCustomerGateway() *schema.Resource {
1616
Update: resourceCloudStackVPNCustomerGatewayUpdate,
1717
Delete: resourceCloudStackVPNCustomerGatewayDelete,
1818
Importer: &schema.ResourceImporter{
19-
State: schema.ImportStatePassthrough,
19+
State: importStatePassthrough,
2020
},
2121

2222
Schema: map[string]*schema.Schema{

0 commit comments

Comments
 (0)