Merge pull request #3419 from rakutentech/add-vsphere-provider

Add VMware vSphere provider
This commit is contained in:
Paul Hinze 2015-10-12 10:13:38 -05:00
commit f77373207c
7 changed files with 1446 additions and 0 deletions

View File

@ -0,0 +1,12 @@
package main
import (
"github.com/hashicorp/terraform/builtin/providers/vsphere"
"github.com/hashicorp/terraform/plugin"
)
func main() {
plugin.Serve(&plugin.ServeOpts{
ProviderFunc: vsphere.Provider,
})
}

View File

@ -0,0 +1 @@
package main

View File

@ -0,0 +1,39 @@
package vsphere
import (
"fmt"
"log"
"net/url"
"github.com/vmware/govmomi"
"golang.org/x/net/context"
)
const (
defaultInsecureFlag = true
)
type Config struct {
User string
Password string
VCenterServer string
}
// Client() returns a new client for accessing VMWare vSphere.
func (c *Config) Client() (*govmomi.Client, error) {
u, err := url.Parse("https://" + c.VCenterServer + "/sdk")
if err != nil {
return nil, fmt.Errorf("Error parse url: %s", err)
}
u.User = url.UserPassword(c.User, c.Password)
client, err := govmomi.NewClient(context.TODO(), u, defaultInsecureFlag)
if err != nil {
return nil, fmt.Errorf("Error setting up client: %s", err)
}
log.Printf("[INFO] VMWare vSphere Client configured for URL: %s", u)
return client, nil
}

View File

@ -0,0 +1,50 @@
package vsphere
import (
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform"
)
// Provider returns a terraform.ResourceProvider.
func Provider() terraform.ResourceProvider {
return &schema.Provider{
Schema: map[string]*schema.Schema{
"user": &schema.Schema{
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("VSPHERE_USER", nil),
Description: "The user name for vSphere API operations.",
},
"password": &schema.Schema{
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("VSPHERE_PASSWORD", nil),
Description: "The user password for vSphere API operations.",
},
"vcenter_server": &schema.Schema{
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("VSPHERE_VCENTER", nil),
Description: "The vCenter Server name for vSphere API operations.",
},
},
ResourcesMap: map[string]*schema.Resource{
"vsphere_virtual_machine": resourceVSphereVirtualMachine(),
},
ConfigureFunc: providerConfigure,
}
}
func providerConfigure(d *schema.ResourceData) (interface{}, error) {
config := Config{
User: d.Get("user").(string),
Password: d.Get("password").(string),
VCenterServer: d.Get("vcenter_server").(string),
}
return config.Client()
}

View File

@ -0,0 +1,43 @@
package vsphere
import (
"os"
"testing"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform"
)
var testAccProviders map[string]terraform.ResourceProvider
var testAccProvider *schema.Provider
func init() {
testAccProvider = Provider().(*schema.Provider)
testAccProviders = map[string]terraform.ResourceProvider{
"vsphere": testAccProvider,
}
}
func TestProvider(t *testing.T) {
if err := Provider().(*schema.Provider).InternalValidate(); err != nil {
t.Fatalf("err: %s", err)
}
}
func TestProvider_impl(t *testing.T) {
var _ terraform.ResourceProvider = Provider()
}
func testAccPreCheck(t *testing.T) {
if v := os.Getenv("VSPHERE_USER"); v == "" {
t.Fatal("VSPHERE_USER must be set for acceptance tests")
}
if v := os.Getenv("VSPHERE_PASSWORD"); v == "" {
t.Fatal("VSPHERE_PASSWORD must be set for acceptance tests")
}
if v := os.Getenv("VSPHERE_VCENTER"); v == "" {
t.Fatal("VSPHERE_VCENTER must be set for acceptance tests")
}
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,240 @@
package vsphere
import (
"fmt"
"os"
"testing"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
"github.com/vmware/govmomi"
"github.com/vmware/govmomi/find"
"github.com/vmware/govmomi/object"
"golang.org/x/net/context"
)
func TestAccVSphereVirtualMachine_basic(t *testing.T) {
var vm virtualMachine
datacenter := os.Getenv("VSPHERE_DATACENTER")
cluster := os.Getenv("VSPHERE_CLUSTER")
datastore := os.Getenv("VSPHERE_DATASTORE")
template := os.Getenv("VSPHERE_TEMPLATE")
gateway := os.Getenv("VSPHERE_NETWORK_GATEWAY")
label := os.Getenv("VSPHERE_NETWORK_LABEL")
ip_address := os.Getenv("VSPHERE_NETWORK_IP_ADDRESS")
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckVSphereVirtualMachineDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: fmt.Sprintf(
testAccCheckVSphereVirtualMachineConfig_basic,
datacenter,
cluster,
gateway,
label,
ip_address,
datastore,
template,
),
Check: resource.ComposeTestCheckFunc(
testAccCheckVSphereVirtualMachineExists("vsphere_virtual_machine.foo", &vm),
resource.TestCheckResourceAttr(
"vsphere_virtual_machine.foo", "name", "terraform-test"),
resource.TestCheckResourceAttr(
"vsphere_virtual_machine.foo", "datacenter", datacenter),
resource.TestCheckResourceAttr(
"vsphere_virtual_machine.foo", "vcpu", "2"),
resource.TestCheckResourceAttr(
"vsphere_virtual_machine.foo", "memory", "4096"),
resource.TestCheckResourceAttr(
"vsphere_virtual_machine.foo", "disk.#", "2"),
resource.TestCheckResourceAttr(
"vsphere_virtual_machine.foo", "disk.0.datastore", datastore),
resource.TestCheckResourceAttr(
"vsphere_virtual_machine.foo", "disk.0.template", template),
resource.TestCheckResourceAttr(
"vsphere_virtual_machine.foo", "network_interface.#", "1"),
resource.TestCheckResourceAttr(
"vsphere_virtual_machine.foo", "network_interface.0.label", label),
),
},
},
})
}
func TestAccVSphereVirtualMachine_dhcp(t *testing.T) {
var vm virtualMachine
datacenter := os.Getenv("VSPHERE_DATACENTER")
cluster := os.Getenv("VSPHERE_CLUSTER")
datastore := os.Getenv("VSPHERE_DATASTORE")
template := os.Getenv("VSPHERE_TEMPLATE")
label := os.Getenv("VSPHERE_NETWORK_LABEL_DHCP")
password := os.Getenv("VSPHERE_VM_PASSWORD")
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckVSphereVirtualMachineDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: fmt.Sprintf(
testAccCheckVSphereVirtualMachineConfig_dhcp,
datacenter,
cluster,
label,
datastore,
template,
password,
),
Check: resource.ComposeTestCheckFunc(
testAccCheckVSphereVirtualMachineExists("vsphere_virtual_machine.bar", &vm),
resource.TestCheckResourceAttr(
"vsphere_virtual_machine.bar", "name", "terraform-test"),
resource.TestCheckResourceAttr(
"vsphere_virtual_machine.bar", "datacenter", datacenter),
resource.TestCheckResourceAttr(
"vsphere_virtual_machine.bar", "vcpu", "2"),
resource.TestCheckResourceAttr(
"vsphere_virtual_machine.bar", "memory", "4096"),
resource.TestCheckResourceAttr(
"vsphere_virtual_machine.bar", "disk.#", "1"),
resource.TestCheckResourceAttr(
"vsphere_virtual_machine.bar", "disk.0.datastore", datastore),
resource.TestCheckResourceAttr(
"vsphere_virtual_machine.bar", "disk.0.template", template),
resource.TestCheckResourceAttr(
"vsphere_virtual_machine.bar", "network_interface.#", "1"),
resource.TestCheckResourceAttr(
"vsphere_virtual_machine.bar", "network_interface.0.label", label),
),
},
},
})
}
func testAccCheckVSphereVirtualMachineDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*govmomi.Client)
finder := find.NewFinder(client.Client, true)
for _, rs := range s.RootModule().Resources {
if rs.Type != "vsphere_virtual_machine" {
continue
}
dc, err := finder.Datacenter(context.TODO(), rs.Primary.Attributes["datacenter"])
if err != nil {
return fmt.Errorf("error %s", err)
}
dcFolders, err := dc.Folders(context.TODO())
if err != nil {
return fmt.Errorf("error %s", err)
}
_, err = object.NewSearchIndex(client.Client).FindChild(context.TODO(), dcFolders.VmFolder, rs.Primary.Attributes["name"])
if err == nil {
return fmt.Errorf("Record still exists")
}
}
return nil
}
func testAccCheckVSphereVirtualMachineExists(n string, vm *virtualMachine) 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("No ID is set")
}
client := testAccProvider.Meta().(*govmomi.Client)
finder := find.NewFinder(client.Client, true)
dc, err := finder.Datacenter(context.TODO(), rs.Primary.Attributes["datacenter"])
if err != nil {
return fmt.Errorf("error %s", err)
}
dcFolders, err := dc.Folders(context.TODO())
if err != nil {
return fmt.Errorf("error %s", err)
}
_, err = object.NewSearchIndex(client.Client).FindChild(context.TODO(), dcFolders.VmFolder, rs.Primary.Attributes["name"])
/*
vmRef, err := client.SearchIndex().FindChild(dcFolders.VmFolder, rs.Primary.Attributes["name"])
if err != nil {
return fmt.Errorf("error %s", err)
}
found := govmomi.NewVirtualMachine(client, vmRef.Reference())
fmt.Printf("%v", found)
if found.Name != rs.Primary.ID {
return fmt.Errorf("Instance not found")
}
*instance = *found
*/
*vm = virtualMachine{
name: rs.Primary.ID,
}
return nil
}
}
const testAccCheckVSphereVirtualMachineConfig_basic = `
resource "vsphere_virtual_machine" "foo" {
name = "terraform-test"
datacenter = "%s"
cluster = "%s"
vcpu = 2
memory = 4096
gateway = "%s"
network_interface {
label = "%s"
ip_address = "%s"
subnet_mask = "255.255.255.0"
}
disk {
datastore = "%s"
template = "%s"
iops = 500
}
disk {
size = 1
iops = 500
}
}
`
const testAccCheckVSphereVirtualMachineConfig_dhcp = `
resource "vsphere_virtual_machine" "bar" {
name = "terraform-test"
datacenter = "%s"
cluster = "%s"
vcpu = 2
memory = 4096
network_interface {
label = "%s"
}
disk {
datastore = "%s"
template = "%s"
}
connection {
host = "${self.network_interface.0.ip_address}"
user = "root"
password = "%s"
}
}
`