-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3419 from rakutentech/add-vsphere-provider
Add VMware vSphere provider
- Loading branch information
Showing
7 changed files
with
1,446 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package main |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |
Oops, something went wrong.