mirror of
https://github.com/opentofu/opentofu.git
synced 2025-02-25 18:45:20 -06:00
Updated to use forked azure-sdk-for-go package
This commit is contained in:
parent
d45f8ac52a
commit
123cd9239c
@ -4,7 +4,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/Azure/azure-sdk-for-go/management"
|
"github.com/svanharmelen/azure-sdk-for-go/management"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Config is the configuration structure used to instantiate a
|
// Config is the configuration structure used to instantiate a
|
||||||
|
@ -6,13 +6,13 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/Azure/azure-sdk-for-go/management"
|
|
||||||
"github.com/Azure/azure-sdk-for-go/management/hostedservice"
|
|
||||||
"github.com/Azure/azure-sdk-for-go/management/osimage"
|
|
||||||
"github.com/Azure/azure-sdk-for-go/management/virtualmachine"
|
|
||||||
"github.com/Azure/azure-sdk-for-go/management/vmutils"
|
|
||||||
"github.com/hashicorp/terraform/helper/hashcode"
|
"github.com/hashicorp/terraform/helper/hashcode"
|
||||||
"github.com/hashicorp/terraform/helper/schema"
|
"github.com/hashicorp/terraform/helper/schema"
|
||||||
|
"github.com/svanharmelen/azure-sdk-for-go/management"
|
||||||
|
"github.com/svanharmelen/azure-sdk-for-go/management/hostedservice"
|
||||||
|
"github.com/svanharmelen/azure-sdk-for-go/management/osimage"
|
||||||
|
"github.com/svanharmelen/azure-sdk-for-go/management/virtualmachine"
|
||||||
|
"github.com/svanharmelen/azure-sdk-for-go/management/vmutils"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -166,165 +166,164 @@ func resourceAzureInstance() *schema.Resource {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func resourceAzureInstanceCreate(d *schema.ResourceData, meta interface{}) (err error) {
|
func resourceAzureInstanceCreate(d *schema.ResourceData, meta interface{}) (err error) {
|
||||||
/*
|
mc := meta.(*management.Client)
|
||||||
mc := meta.(*management.Client)
|
|
||||||
|
|
||||||
name := d.Get("name").(string)
|
name := d.Get("name").(string)
|
||||||
|
|
||||||
// Compute/set the description
|
// Compute/set the description
|
||||||
description := d.Get("description").(string)
|
description := d.Get("description").(string)
|
||||||
if description == "" {
|
if description == "" {
|
||||||
description = name
|
description = name
|
||||||
|
}
|
||||||
|
|
||||||
|
// Retrieve the needed details of the image
|
||||||
|
imageName, imageURL, osType, err := retrieveImageDetails(mc, d.Get("image").(string))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if imageURL == "" {
|
||||||
|
storage, ok := d.GetOk("storage")
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("When using a platform image, the 'storage' parameter is required")
|
||||||
}
|
}
|
||||||
|
imageURL = fmt.Sprintf("http://%s.blob.core.windows.net/vhds/%s.vhd", storage, name)
|
||||||
|
}
|
||||||
|
|
||||||
// Retrieve the needed details of the image
|
// Verify if we have all parameters required for the image OS type
|
||||||
imageName, imageURL, osType, err := retrieveImageDetails(mc, d.Get("image").(string))
|
if err := verifyParameters(d, osType); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("[DEBUG] Creating Cloud Service for instance: %s", name)
|
||||||
|
req, err := hostedservice.NewClient(*mc).
|
||||||
|
CreateHostedService(
|
||||||
|
name,
|
||||||
|
d.Get("location").(string),
|
||||||
|
d.Get("reverse_dns").(string),
|
||||||
|
name,
|
||||||
|
fmt.Sprintf("Cloud Service created automatically for instance %s", name),
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("Error creating Cloud Service for instance %s: %s", name, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wait until the Cloud Service is created
|
||||||
|
if err := mc.WaitAsyncOperation(req); err != nil {
|
||||||
|
return fmt.Errorf(
|
||||||
|
"Error waiting for Cloud Service of instance %s to be created: %s", name, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Put in this defer here, so we are sure to cleanup already created parts
|
||||||
|
// when we exit with an error
|
||||||
|
defer func(mc *management.Client) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
req, err := hostedservice.NewClient(*mc).DeleteHostedService(name, true)
|
||||||
}
|
|
||||||
|
|
||||||
if imageURL == "" {
|
|
||||||
storage, ok := d.GetOk("storage")
|
|
||||||
if !ok {
|
|
||||||
return fmt.Errorf("When using a platform image, the 'storage' parameter is required")
|
|
||||||
}
|
|
||||||
imageURL = fmt.Sprintf("http://%s.blob.core.windows.net/vhds/%s.vhd", storage, name)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Verify if we have all parameters required for the image OS type
|
|
||||||
if err := verifyParameters(d, osType); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Printf("[DEBUG] Creating Cloud Service for instance: %s", name)
|
|
||||||
req, err := hostedservice.NewClient(*mc).
|
|
||||||
CreateHostedService(
|
|
||||||
name,
|
|
||||||
d.Get("location").(string),
|
|
||||||
d.Get("reverse_dns").(string),
|
|
||||||
name,
|
|
||||||
fmt.Sprintf("Cloud Service created automatically for instance %s", name),
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("Error creating Cloud Service for instance %s: %s", name, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Wait until the Cloud Service is created
|
|
||||||
if err := mc.WaitAsyncOperation(req); err != nil {
|
|
||||||
return fmt.Errorf(
|
|
||||||
"Error waiting for Cloud Service of instance %s to be created: %s", name, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Put in this defer here, so we are sure to cleanup already created parts
|
|
||||||
// when we exit with an error
|
|
||||||
defer func(mc *management.Client) {
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
req, err := hostedservice.NewClient(*mc).DeleteHostedService(name, true)
|
log.Printf("[DEBUG] Error cleaning up Cloud Service of instance %s: %s", name, err)
|
||||||
if err != nil {
|
|
||||||
log.Printf("[DEBUG] Error cleaning up Cloud Service of instance %s: %s", name, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Wait until the Cloud Service is deleted
|
|
||||||
if err := mc.WaitAsyncOperation(req); err != nil {
|
|
||||||
log.Printf(
|
|
||||||
"[DEBUG] Error waiting for Cloud Service of instance %s to be deleted : %s", name, err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}(mc)
|
|
||||||
|
|
||||||
// Create a new role for the instance
|
// Wait until the Cloud Service is deleted
|
||||||
role := vmutils.NewVmConfiguration(name, d.Get("size").(string))
|
if err := mc.WaitAsyncOperation(req); err != nil {
|
||||||
|
log.Printf(
|
||||||
log.Printf("[DEBUG] Configuring deployment from image...")
|
"[DEBUG] Error waiting for Cloud Service of instance %s to be deleted : %s", name, err)
|
||||||
err = vmutils.ConfigureDeploymentFromPlatformImage(
|
|
||||||
&role,
|
|
||||||
imageName,
|
|
||||||
imageURL,
|
|
||||||
d.Get("image").(string),
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("Error configuring the deployment for %s: %s", name, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if osType == linux {
|
|
||||||
// This is pretty ugly, but the Azure SDK leaves me no other choice...
|
|
||||||
if tp, ok := d.GetOk("ssh_key_thumbprint"); ok {
|
|
||||||
err = vmutils.ConfigureForLinux(
|
|
||||||
&role,
|
|
||||||
name,
|
|
||||||
d.Get("username").(string),
|
|
||||||
d.Get("password").(string),
|
|
||||||
tp.(string),
|
|
||||||
)
|
|
||||||
} else {
|
|
||||||
err = vmutils.ConfigureForLinux(
|
|
||||||
&role,
|
|
||||||
name,
|
|
||||||
d.Get("username").(string),
|
|
||||||
d.Get("password").(string),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("Error configuring %s for Linux: %s", name, err)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}(mc)
|
||||||
|
|
||||||
if osType == windows {
|
// Create a new role for the instance
|
||||||
err = vmutils.ConfigureForWindows(
|
role := vmutils.NewVmConfiguration(name, d.Get("size").(string))
|
||||||
|
|
||||||
|
log.Printf("[DEBUG] Configuring deployment from image...")
|
||||||
|
err = vmutils.ConfigureDeploymentFromPlatformImage(
|
||||||
|
&role,
|
||||||
|
imageName,
|
||||||
|
imageURL,
|
||||||
|
d.Get("image").(string),
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("Error configuring the deployment for %s: %s", name, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if osType == linux {
|
||||||
|
// This is pretty ugly, but the Azure SDK leaves me no other choice...
|
||||||
|
if tp, ok := d.GetOk("ssh_key_thumbprint"); ok {
|
||||||
|
err = vmutils.ConfigureForLinux(
|
||||||
&role,
|
&role,
|
||||||
name,
|
name,
|
||||||
d.Get("username").(string),
|
d.Get("username").(string),
|
||||||
d.Get("password").(string),
|
d.Get("password").(string),
|
||||||
d.Get("automatic_updates").(bool),
|
tp.(string),
|
||||||
d.Get("time_zone").(string),
|
)
|
||||||
|
} else {
|
||||||
|
err = vmutils.ConfigureForLinux(
|
||||||
|
&role,
|
||||||
|
name,
|
||||||
|
d.Get("username").(string),
|
||||||
|
d.Get("password").(string),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("Error configuring %s for Linux: %s", name, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if osType == windows {
|
||||||
|
err = vmutils.ConfigureForWindows(
|
||||||
|
&role,
|
||||||
|
name,
|
||||||
|
d.Get("username").(string),
|
||||||
|
d.Get("password").(string),
|
||||||
|
d.Get("automatic_updates").(bool),
|
||||||
|
d.Get("time_zone").(string),
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("Error configuring %s for Windows: %s", name, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if s := d.Get("endpoint").(*schema.Set); s.Len() > 0 {
|
||||||
|
for _, v := range s.List() {
|
||||||
|
m := v.(map[string]interface{})
|
||||||
|
err := vmutils.ConfigureWithExternalPort(
|
||||||
|
&role,
|
||||||
|
m["name"].(string),
|
||||||
|
m["private_port"].(int),
|
||||||
|
m["public_port"].(int),
|
||||||
|
endpointProtocol(m["protocol"].(string)),
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("Error configuring %s for Windows: %s", name, err)
|
return fmt.Errorf(
|
||||||
|
"Error adding endpoint %s for instance %s: %s", m["name"].(string), name, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if s := d.Get("endpoint").(*schema.Set); s.Len() > 0 {
|
err = vmutils.ConfigureForSubnet(&role, d.Get("subnet").(string))
|
||||||
for _, v := range s.List() {
|
if err != nil {
|
||||||
m := v.(map[string]interface{})
|
return fmt.Errorf(
|
||||||
err := vmutils.ConfigureWithExternalPort(
|
"Error adding role to subnet %s for instance %s: %s", d.Get("subnet").(string), name, err)
|
||||||
&role,
|
}
|
||||||
m["name"].(string),
|
|
||||||
m["private_port"].(int),
|
|
||||||
m["public_port"].(int),
|
|
||||||
endpointProtocol(m["protocol"].(string)),
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf(
|
|
||||||
"Error adding endpoint %s for instance %s: %s", m["name"].(string), name, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
err = vmutils.ConfigureForSubnet(&role, d.Get("subnet").(string))
|
options := virtualmachine.CreateDeploymentOptions{
|
||||||
if err != nil {
|
Subnet: d.Get("subnet").(string),
|
||||||
return fmt.Errorf(
|
VirtualNetworkName: d.Get("virtual_network").(string),
|
||||||
"Error adding role to subnet %s for instance %s: %s", d.Get("subnet").(string), name, err)
|
}
|
||||||
}
|
|
||||||
|
|
||||||
options := &virtualmachine.CreateDeploymentOptions{
|
log.Printf("[DEBUG] Creating the new instance...")
|
||||||
Subnet: d.Get("subnet").(string),
|
req, err = virtualmachine.NewClient(*mc).CreateDeployment(role, name, options)
|
||||||
VirtualNetworkName: d.Get("virtual_network").(string),
|
if err != nil {
|
||||||
}
|
return fmt.Errorf("Error creating instance %s: %s", name, err)
|
||||||
|
}
|
||||||
|
|
||||||
log.Printf("[DEBUG] Creating the new instance...")
|
log.Printf("[DEBUG] Waiting for the new instance to be created...")
|
||||||
req, err = virtualmachine.NewClient(*mc).CreateDeployment(role, name, options)
|
if err := mc.WaitAsyncOperation(req); err != nil {
|
||||||
if err != nil {
|
return fmt.Errorf(
|
||||||
return fmt.Errorf("Error creating instance %s: %s", name, err)
|
"Error waiting for instance %s to be created: %s", name, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("[DEBUG] Waiting for the new instance to be created...")
|
d.SetId(name)
|
||||||
if err := mc.WaitAsyncOperation(req); err != nil {
|
|
||||||
return fmt.Errorf(
|
|
||||||
"Error waiting for instance %s to be created: %s", name, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
d.SetId(name)
|
|
||||||
*/
|
|
||||||
return resourceAzureInstanceRead(d, meta)
|
return resourceAzureInstanceRead(d, meta)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,9 +5,9 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"github.com/Azure/azure-sdk-for-go/management"
|
|
||||||
"github.com/Azure/azure-sdk-for-go/management/networksecuritygroup"
|
|
||||||
"github.com/hashicorp/terraform/helper/schema"
|
"github.com/hashicorp/terraform/helper/schema"
|
||||||
|
"github.com/svanharmelen/azure-sdk-for-go/management"
|
||||||
|
"github.com/svanharmelen/azure-sdk-for-go/management/networksecuritygroup"
|
||||||
)
|
)
|
||||||
|
|
||||||
func resourceAzureSecurityGroup() *schema.Resource {
|
func resourceAzureSecurityGroup() *schema.Resource {
|
||||||
|
@ -5,10 +5,10 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/Azure/azure-sdk-for-go/management"
|
|
||||||
"github.com/Azure/azure-sdk-for-go/management/virtualnetwork"
|
|
||||||
"github.com/hashicorp/terraform/helper/schema"
|
"github.com/hashicorp/terraform/helper/schema"
|
||||||
"github.com/mitchellh/mapstructure"
|
"github.com/mitchellh/mapstructure"
|
||||||
|
"github.com/svanharmelen/azure-sdk-for-go/management"
|
||||||
|
"github.com/svanharmelen/azure-sdk-for-go/management/virtualnetwork"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
Loading…
Reference in New Issue
Block a user