Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add VMware vSphere provider #3419

Merged
merged 2 commits into from
Oct 12, 2015
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
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
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a security hole.
SSL certificate validation must be switched on by default, and has to be disabled by a user explicitly.

)

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),
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd rename it to VSPHERE_SERVER

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