Skip to content

Commit

Permalink
Merge pull request #3419 from rakutentech/add-vsphere-provider
Browse files Browse the repository at this point in the history
Add VMware vSphere provider
  • Loading branch information
phinze committed Oct 12, 2015
2 parents 299b673 + f34628d commit f773732
Show file tree
Hide file tree
Showing 7 changed files with 1,446 additions and 0 deletions.
12 changes: 12 additions & 0 deletions builtin/bins/provider-vsphere/main.go
Original file line number Diff line number Diff line change
@@ -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,
})
}
1 change: 1 addition & 0 deletions builtin/bins/provider-vsphere/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package main
39 changes: 39 additions & 0 deletions builtin/providers/vsphere/config.go
Original file line number Diff line number Diff line change
@@ -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
}
50 changes: 50 additions & 0 deletions builtin/providers/vsphere/provider.go
Original file line number Diff line number Diff line change
@@ -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()
}
43 changes: 43 additions & 0 deletions builtin/providers/vsphere/provider_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
}
Loading

0 comments on commit f773732

Please sign in to comment.