-
Notifications
You must be signed in to change notification settings - Fork 9.6k
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
WIP: Add Azure provider (peer-review PR) #2053
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
84a870a
First few azure resources...
4e33d89
Needs more testing and tests, but it's becoming a nice provider
f8a56ad
Little refactoring and fixing some issues
d45f8ac
Small update after changes in the Azure SDK
123cd92
Updated to use forked azure-sdk-for-go package
ca1eb19
Adding docs and tweaking the provider
1dbd32c
Extending instance resource to enable using custom VM images
83e3ab1
Seems to be almost ready...
8c3cb40
Updated to work with the latest Azure SDK changes
a2aeb9f
Adding acceptance tests together with a few minor tweaks
cef8259
Adding the last parts of the docs for the new Azure provider
08dd7de
Very minor change needed to realign with the updated Azure SDK
120599e
Add some additional checks
7e388bb
Make sure we expand the homedir
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/azure" | ||
"github.com/hashicorp/terraform/plugin" | ||
) | ||
|
||
func main() { | ||
plugin.Serve(&plugin.ServeOpts{ | ||
ProviderFunc: azure.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,58 @@ | ||
package azure | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"sync" | ||
|
||
"github.com/svanharmelen/azure-sdk-for-go/management" | ||
) | ||
|
||
// Config is the configuration structure used to instantiate a | ||
// new Azure management client. | ||
type Config struct { | ||
SettingsFile string | ||
SubscriptionID string | ||
Certificate []byte | ||
ManagementURL string | ||
} | ||
|
||
// Client contains all the handles required for managing Azure services. | ||
type Client struct { | ||
// unfortunately; because of how Azure's network API works; doing networking operations | ||
// concurrently is very hazardous, and we need a mutex to guard the management.Client. | ||
mutex *sync.Mutex | ||
mgmtClient management.Client | ||
} | ||
|
||
// NewClientFromSettingsFile returns a new Azure management | ||
// client created using a publish settings file. | ||
func (c *Config) NewClientFromSettingsFile() (*Client, error) { | ||
if _, err := os.Stat(c.SettingsFile); os.IsNotExist(err) { | ||
return nil, fmt.Errorf("Publish Settings file %q does not exist!", c.SettingsFile) | ||
} | ||
|
||
mc, err := management.ClientFromPublishSettingsFile(c.SettingsFile, c.SubscriptionID) | ||
if err != nil { | ||
return nil, nil | ||
} | ||
|
||
return &Client{ | ||
mutex: &sync.Mutex{}, | ||
mgmtClient: mc, | ||
}, nil | ||
} | ||
|
||
// NewClient returns a new Azure management client created | ||
// using a subscription ID and certificate. | ||
func (c *Config) NewClient() (*Client, error) { | ||
mc, err := management.NewClient(c.SubscriptionID, c.Certificate) | ||
if err != nil { | ||
return nil, nil | ||
} | ||
|
||
return &Client{ | ||
mutex: &sync.Mutex{}, | ||
mgmtClient: mc, | ||
}, 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,68 @@ | ||
package azure | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/hashicorp/terraform/helper/schema" | ||
"github.com/hashicorp/terraform/terraform" | ||
"github.com/mitchellh/go-homedir" | ||
) | ||
|
||
// Provider returns a terraform.ResourceProvider. | ||
func Provider() terraform.ResourceProvider { | ||
return &schema.Provider{ | ||
Schema: map[string]*schema.Schema{ | ||
"settings_file": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Optional: true, | ||
DefaultFunc: schema.EnvDefaultFunc("AZURE_SETTINGS_FILE", nil), | ||
}, | ||
|
||
"subscription_id": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Optional: true, | ||
DefaultFunc: schema.EnvDefaultFunc("AZURE_SUBSCRIPTION_ID", ""), | ||
}, | ||
|
||
"certificate": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Optional: true, | ||
DefaultFunc: schema.EnvDefaultFunc("AZURE_CERTIFICATE", ""), | ||
}, | ||
}, | ||
|
||
ResourcesMap: map[string]*schema.Resource{ | ||
"azure_data_disk": resourceAzureDataDisk(), | ||
"azure_instance": resourceAzureInstance(), | ||
"azure_security_group": resourceAzureSecurityGroup(), | ||
"azure_virtual_network": resourceAzureVirtualNetwork(), | ||
}, | ||
|
||
ConfigureFunc: providerConfigure, | ||
} | ||
} | ||
|
||
func providerConfigure(d *schema.ResourceData) (interface{}, error) { | ||
settingsFile, err := homedir.Expand(d.Get("settings_file").(string)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
config := Config{ | ||
SettingsFile: settingsFile, | ||
SubscriptionID: d.Get("subscription_id").(string), | ||
Certificate: []byte(d.Get("certificate").(string)), | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you do a homedir expand on the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated, will commit in a second... |
||
|
||
if config.SettingsFile != "" { | ||
return config.NewClientFromSettingsFile() | ||
} | ||
|
||
if config.SubscriptionID != "" && len(config.Certificate) > 0 { | ||
return config.NewClient() | ||
} | ||
|
||
return nil, fmt.Errorf( | ||
"Insufficient configuration data. Please specify either a 'settings_file'\n" + | ||
"or both a 'subscription_id' and 'certificate'.") | ||
} |
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,45 @@ | ||
package azure | ||
|
||
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{ | ||
"azure": 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("AZURE_SETTINGS_FILE"); v == "" { | ||
subscriptionID := os.Getenv("AZURE_SUBSCRIPTION_ID") | ||
certificate := os.Getenv("AZURE_CERTIFICATE") | ||
|
||
if subscriptionID == "" || certificate == "" { | ||
t.Fatal("either AZURE_SETTINGS_FILE, or AZURE_SUBSCRIPTION_ID " + | ||
"and AZURE_CERTIFICATE must be set for acceptance tests") | ||
} | ||
} | ||
|
||
if v := os.Getenv("AZURE_STORAGE"); v == "" { | ||
t.Fatal("AZURE_STORAGE must be set for acceptance tests") | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would have been nice to provide the id + certificate + url authentication option as well for people with Windows Azure Packs.