github.com/richardbowden/terraform@v0.6.12-0.20160901200758-30ea22c25211/builtin/providers/triton/provider.go (about) 1 package triton 2 3 import ( 4 "fmt" 5 "log" 6 "os" 7 8 "github.com/hashicorp/go-multierror" 9 "github.com/hashicorp/terraform/helper/schema" 10 "github.com/hashicorp/terraform/terraform" 11 "github.com/joyent/gocommon/client" 12 "github.com/joyent/gosdc/cloudapi" 13 "github.com/joyent/gosign/auth" 14 ) 15 16 // Provider returns a terraform.ResourceProvider. 17 func Provider() terraform.ResourceProvider { 18 return &schema.Provider{ 19 Schema: map[string]*schema.Schema{ 20 "account": &schema.Schema{ 21 Type: schema.TypeString, 22 Required: true, 23 DefaultFunc: schema.EnvDefaultFunc("SDC_ACCOUNT", ""), 24 }, 25 26 "url": &schema.Schema{ 27 Type: schema.TypeString, 28 Required: true, 29 DefaultFunc: schema.EnvDefaultFunc("SDC_URL", "https://us-west-1.api.joyentcloud.com"), 30 }, 31 32 "key_material": &schema.Schema{ 33 Type: schema.TypeString, 34 Required: true, 35 DefaultFunc: schema.EnvDefaultFunc("SDC_KEY_MATERIAL", ""), 36 }, 37 38 "key_id": &schema.Schema{ 39 Type: schema.TypeString, 40 Required: true, 41 DefaultFunc: schema.EnvDefaultFunc("SDC_KEY_ID", ""), 42 }, 43 }, 44 45 ResourcesMap: map[string]*schema.Resource{ 46 "triton_firewall_rule": resourceFirewallRule(), 47 "triton_machine": resourceMachine(), 48 "triton_key": resourceKey(), 49 "triton_vlan": resourceVLAN(), 50 "triton_fabric": resourceFabric(), 51 }, 52 ConfigureFunc: providerConfigure, 53 } 54 } 55 56 type SDCConfig struct { 57 Account string 58 KeyMaterial string 59 KeyID string 60 URL string 61 } 62 63 func (c SDCConfig) validate() error { 64 var err *multierror.Error 65 66 if c.URL == "" { 67 err = multierror.Append(err, fmt.Errorf("URL must be configured for the Triton provider")) 68 } 69 if c.KeyMaterial == "" { 70 err = multierror.Append(err, fmt.Errorf("Key Material must be configured for the Triton provider")) 71 } 72 if c.KeyID == "" { 73 err = multierror.Append(err, fmt.Errorf("Key ID must be configured for the Triton provider")) 74 } 75 if c.Account == "" { 76 err = multierror.Append(err, fmt.Errorf("Account must be configured for the Triton provider")) 77 } 78 79 return err.ErrorOrNil() 80 } 81 82 func (c SDCConfig) getSDCClient() (*cloudapi.Client, error) { 83 userauth, err := auth.NewAuth(c.Account, c.KeyMaterial, "rsa-sha256") 84 if err != nil { 85 return nil, err 86 } 87 88 creds := &auth.Credentials{ 89 UserAuthentication: userauth, 90 SdcKeyId: c.KeyID, 91 SdcEndpoint: auth.Endpoint{URL: c.URL}, 92 } 93 94 client := cloudapi.New(client.NewClient( 95 c.URL, 96 cloudapi.DefaultAPIVersion, 97 creds, 98 log.New(os.Stderr, "", log.LstdFlags), 99 )) 100 101 return client, nil 102 } 103 104 func providerConfigure(d *schema.ResourceData) (interface{}, error) { 105 config := SDCConfig{ 106 Account: d.Get("account").(string), 107 URL: d.Get("url").(string), 108 KeyMaterial: d.Get("key_material").(string), 109 KeyID: d.Get("key_id").(string), 110 } 111 112 if err := config.validate(); err != nil { 113 return nil, err 114 } 115 116 client, err := config.getSDCClient() 117 if err != nil { 118 return nil, err 119 } 120 121 return client, nil 122 }