github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/vsphere/resource_vsphere_folder_test.go (about) 1 package vsphere 2 3 import ( 4 "fmt" 5 "os" 6 "testing" 7 8 "github.com/hashicorp/terraform/helper/resource" 9 "github.com/hashicorp/terraform/terraform" 10 "github.com/vmware/govmomi" 11 "github.com/vmware/govmomi/find" 12 "github.com/vmware/govmomi/object" 13 "golang.org/x/net/context" 14 ) 15 16 // Basic top-level folder creation 17 func TestAccVSphereFolder_basic(t *testing.T) { 18 var f folder 19 datacenter := os.Getenv("VSPHERE_DATACENTER") 20 testMethod := "basic" 21 resourceName := "vsphere_folder." + testMethod 22 path := "tf_test_basic" 23 24 resource.Test(t, resource.TestCase{ 25 PreCheck: func() { testAccPreCheck(t) }, 26 Providers: testAccProviders, 27 CheckDestroy: testAccCheckVSphereFolderDestroy, 28 Steps: []resource.TestStep{ 29 resource.TestStep{ 30 Config: fmt.Sprintf( 31 testAccCheckVSphereFolderConfig, 32 testMethod, 33 path, 34 datacenter, 35 ), 36 Check: resource.ComposeTestCheckFunc( 37 testAccCheckVSphereFolderExists(resourceName, &f), 38 resource.TestCheckResourceAttr( 39 resourceName, "path", path), 40 resource.TestCheckResourceAttr( 41 resourceName, "existing_path", ""), 42 ), 43 }, 44 }, 45 }) 46 } 47 48 func TestAccVSphereFolder_nested(t *testing.T) { 49 50 var f folder 51 datacenter := os.Getenv("VSPHERE_DATACENTER") 52 testMethod := "nested" 53 resourceName := "vsphere_folder." + testMethod 54 path := "tf_test_nested/tf_test_folder" 55 56 resource.Test(t, resource.TestCase{ 57 PreCheck: func() { testAccPreCheck(t) }, 58 Providers: testAccProviders, 59 CheckDestroy: testAccCheckVSphereFolderDestroy, 60 Steps: []resource.TestStep{ 61 resource.TestStep{ 62 Config: fmt.Sprintf( 63 testAccCheckVSphereFolderConfig, 64 testMethod, 65 path, 66 datacenter, 67 ), 68 Check: resource.ComposeTestCheckFunc( 69 testAccCheckVSphereFolderExists(resourceName, &f), 70 resource.TestCheckResourceAttr( 71 resourceName, "path", path), 72 resource.TestCheckResourceAttr( 73 resourceName, "existing_path", ""), 74 ), 75 }, 76 }, 77 }) 78 } 79 80 func TestAccVSphereFolder_dontDeleteExisting(t *testing.T) { 81 82 var f folder 83 datacenter := os.Getenv("VSPHERE_DATACENTER") 84 testMethod := "dontDeleteExisting" 85 resourceName := "vsphere_folder." + testMethod 86 existingPath := "tf_test_dontDeleteExisting/tf_existing" 87 path := existingPath + "/tf_nested/tf_test" 88 89 resource.Test(t, resource.TestCase{ 90 PreCheck: func() { testAccPreCheck(t) }, 91 Providers: testAccProviders, 92 CheckDestroy: resource.ComposeTestCheckFunc( 93 assertVSphereFolderExists(datacenter, existingPath), 94 removeVSphereFolder(datacenter, existingPath, ""), 95 ), 96 Steps: []resource.TestStep{ 97 resource.TestStep{ 98 PreConfig: func() { 99 createVSphereFolder(datacenter, existingPath) 100 }, 101 Config: fmt.Sprintf( 102 testAccCheckVSphereFolderConfig, 103 testMethod, 104 path, 105 datacenter, 106 ), 107 Check: resource.ComposeTestCheckFunc( 108 testAccCheckVSphereFolderExistingPathExists(resourceName, &f), 109 resource.TestCheckResourceAttr( 110 resourceName, "path", path), 111 resource.TestCheckResourceAttr( 112 resourceName, "existing_path", existingPath), 113 ), 114 }, 115 }, 116 }) 117 } 118 119 func testAccCheckVSphereFolderDestroy(s *terraform.State) error { 120 client := testAccProvider.Meta().(*govmomi.Client) 121 finder := find.NewFinder(client.Client, true) 122 123 for _, rs := range s.RootModule().Resources { 124 if rs.Type != "vsphere_folder" { 125 continue 126 } 127 128 dc, err := finder.Datacenter(context.TODO(), rs.Primary.Attributes["datacenter"]) 129 if err != nil { 130 return fmt.Errorf("error %s", err) 131 } 132 133 dcFolders, err := dc.Folders(context.TODO()) 134 if err != nil { 135 return fmt.Errorf("error %s", err) 136 } 137 138 f, err := object.NewSearchIndex(client.Client).FindChild(context.TODO(), dcFolders.VmFolder, rs.Primary.Attributes["path"]) 139 if f != nil { 140 return fmt.Errorf("Record still exists") 141 } 142 } 143 144 return nil 145 } 146 147 func testAccCheckVSphereFolderExists(n string, f *folder) resource.TestCheckFunc { 148 return func(s *terraform.State) error { 149 rs, ok := s.RootModule().Resources[n] 150 if !ok { 151 return fmt.Errorf("Resource not found: %s", n) 152 } 153 154 if rs.Primary.ID == "" { 155 return fmt.Errorf("No ID is set") 156 } 157 158 client := testAccProvider.Meta().(*govmomi.Client) 159 finder := find.NewFinder(client.Client, true) 160 161 dc, err := finder.Datacenter(context.TODO(), rs.Primary.Attributes["datacenter"]) 162 if err != nil { 163 return fmt.Errorf("error %s", err) 164 } 165 166 dcFolders, err := dc.Folders(context.TODO()) 167 if err != nil { 168 return fmt.Errorf("error %s", err) 169 } 170 171 _, err = object.NewSearchIndex(client.Client).FindChild(context.TODO(), dcFolders.VmFolder, rs.Primary.Attributes["path"]) 172 173 *f = folder{ 174 path: rs.Primary.Attributes["path"], 175 } 176 177 return nil 178 } 179 } 180 181 func testAccCheckVSphereFolderExistingPathExists(n string, f *folder) resource.TestCheckFunc { 182 return func(s *terraform.State) error { 183 rs, ok := s.RootModule().Resources[n] 184 if !ok { 185 return fmt.Errorf("Resource %s not found in %#v", n, s.RootModule().Resources) 186 } 187 188 if rs.Primary.ID == "" { 189 return fmt.Errorf("No ID is set") 190 } 191 192 client := testAccProvider.Meta().(*govmomi.Client) 193 finder := find.NewFinder(client.Client, true) 194 195 dc, err := finder.Datacenter(context.TODO(), rs.Primary.Attributes["datacenter"]) 196 if err != nil { 197 return fmt.Errorf("error %s", err) 198 } 199 200 dcFolders, err := dc.Folders(context.TODO()) 201 if err != nil { 202 return fmt.Errorf("error %s", err) 203 } 204 205 _, err = object.NewSearchIndex(client.Client).FindChild(context.TODO(), dcFolders.VmFolder, rs.Primary.Attributes["existing_path"]) 206 207 *f = folder{ 208 path: rs.Primary.Attributes["path"], 209 } 210 211 return nil 212 } 213 } 214 215 func assertVSphereFolderExists(datacenter string, folder_name string) resource.TestCheckFunc { 216 217 return func(s *terraform.State) error { 218 client := testAccProvider.Meta().(*govmomi.Client) 219 folder, err := object.NewSearchIndex(client.Client).FindByInventoryPath( 220 context.TODO(), fmt.Sprintf("%v/vm/%v", datacenter, folder_name)) 221 if err != nil { 222 return fmt.Errorf("Error: %s", err) 223 } else if folder == nil { 224 return fmt.Errorf("Folder %s does not exist!", folder_name) 225 } 226 227 return nil 228 } 229 } 230 231 func createVSphereFolder(datacenter string, folder_name string) error { 232 233 client := testAccProvider.Meta().(*govmomi.Client) 234 235 f := folder{path: folder_name, datacenter: datacenter} 236 237 folder, err := object.NewSearchIndex(client.Client).FindByInventoryPath( 238 context.TODO(), fmt.Sprintf("%v/vm/%v", datacenter, folder_name)) 239 if err != nil { 240 return fmt.Errorf("error %s", err) 241 } 242 243 if folder == nil { 244 createFolder(client, &f) 245 } else { 246 return fmt.Errorf("Folder %s already exists", folder_name) 247 } 248 249 return nil 250 } 251 252 func removeVSphereFolder(datacenter string, folder_name string, existing_path string) resource.TestCheckFunc { 253 254 f := folder{path: folder_name, datacenter: datacenter, existingPath: existing_path} 255 256 return func(s *terraform.State) error { 257 258 client := testAccProvider.Meta().(*govmomi.Client) 259 // finder := find.NewFinder(client.Client, true) 260 261 folder, _ := object.NewSearchIndex(client.Client).FindByInventoryPath( 262 context.TODO(), fmt.Sprintf("%v/vm/%v", datacenter, folder_name)) 263 if folder != nil { 264 deleteFolder(client, &f) 265 } 266 267 return nil 268 } 269 } 270 271 const testAccCheckVSphereFolderConfig = ` 272 resource "vsphere_folder" "%s" { 273 path = "%s" 274 datacenter = "%s" 275 } 276 `