-
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.
- Loading branch information
Showing
7 changed files
with
186 additions
and
13 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,46 @@ | ||
package aws | ||
|
||
import ( | ||
"strings" | ||
"unicode" | ||
|
||
"github.com/mitchellh/goamz/aws" | ||
) | ||
|
||
type Config struct { | ||
AccessKey string `mapstructure:"access_key"` | ||
SecretKey string `mapstructure:"secret_key"` | ||
Region string `mapstructure:"region"` | ||
} | ||
|
||
// AWSAuth returns a valid aws.Auth object for access to AWS services, or | ||
// an error if the authentication couldn't be resolved. | ||
// | ||
// TODO(mitchellh): Test in some way. | ||
func (c *Config) AWSAuth() (aws.Auth, error) { | ||
auth, err := aws.GetAuth(c.AccessKey, c.SecretKey) | ||
if err == nil { | ||
// Store the accesskey and secret that we got... | ||
c.AccessKey = auth.AccessKey | ||
c.SecretKey = auth.SecretKey | ||
} | ||
|
||
return auth, err | ||
} | ||
|
||
// AWSRegion returns the configured region. | ||
// | ||
// TODO(mitchellh): Test in some way. | ||
func (c *Config) AWSRegion() (aws.Region, error) { | ||
if c.Region != "" { | ||
return aws.Regions[c.Region], nil | ||
} | ||
|
||
md, err := aws.GetMetaData("placement/availability-zone") | ||
if err != nil { | ||
return aws.Region{}, err | ||
} | ||
|
||
region := strings.TrimRightFunc(string(md), unicode.IsLetter) | ||
return aws.Regions[region], 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
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,31 @@ | ||
package aws | ||
|
||
import ( | ||
"github.com/hashicorp/terraform/terraform" | ||
) | ||
|
||
// RefreshFunc is a function that performs a refresh of a specific type | ||
// of resource. | ||
type RefreshFunc func( | ||
*ResourceProvider, | ||
*terraform.ResourceState) (*terraform.ResourceState, error) | ||
|
||
// refreshMap keeps track of all the resources that this provider | ||
// can refresh. | ||
var refreshMap map[string]RefreshFunc | ||
|
||
func init() { | ||
refreshMap = map[string]RefreshFunc{ | ||
"aws_instance": refresh_aws_instance, | ||
} | ||
} | ||
|
||
func refresh_aws_instance( | ||
p *ResourceProvider, | ||
s *terraform.ResourceState) (*terraform.ResourceState, error) { | ||
if s.ID != "" { | ||
panic("OH MY WOW") | ||
} | ||
|
||
return s, 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
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 |
---|---|---|
@@ -1,11 +1,43 @@ | ||
package aws | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform/config" | ||
"github.com/hashicorp/terraform/terraform" | ||
) | ||
|
||
func TestResourceProvider_impl(t *testing.T) { | ||
var _ terraform.ResourceProvider = new(ResourceProvider) | ||
} | ||
|
||
func TestResourceProvider_Configure(t *testing.T) { | ||
rp := new(ResourceProvider) | ||
|
||
raw := map[string]interface{}{ | ||
"access_key": "foo", | ||
"secret_key": "bar", | ||
"region": "us-east", | ||
} | ||
|
||
rawConfig, err := config.NewRawConfig(raw) | ||
if err != nil { | ||
t.Fatalf("err: %s", err) | ||
} | ||
|
||
err = rp.Configure(terraform.NewResourceConfig(rawConfig)) | ||
if err != nil { | ||
t.Fatalf("err: %s", err) | ||
} | ||
|
||
expected := Config{ | ||
AccessKey: "foo", | ||
SecretKey: "bar", | ||
Region: "us-east", | ||
} | ||
|
||
if !reflect.DeepEqual(rp.Config, expected) { | ||
t.Fatalf("bad: %#v", rp.Config) | ||
} | ||
} |
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,28 @@ | ||
package config | ||
|
||
import ( | ||
"github.com/mitchellh/mapstructure" | ||
) | ||
|
||
func Decode(target interface{}, raws ...interface{}) (*mapstructure.Metadata, error) { | ||
var md mapstructure.Metadata | ||
decoderConfig := &mapstructure.DecoderConfig{ | ||
Metadata: &md, | ||
Result: target, | ||
WeaklyTypedInput: true, | ||
} | ||
|
||
decoder, err := mapstructure.NewDecoder(decoderConfig) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
for _, raw := range raws { | ||
err := decoder.Decode(raw) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
return &md, 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