Skip to content

Commit

Permalink
Updated with changes in sdk.
Browse files Browse the repository at this point in the history
  • Loading branch information
aznashwan committed May 22, 2015
1 parent 376574a commit a0a86ae
Show file tree
Hide file tree
Showing 8 changed files with 180 additions and 133 deletions.
2 changes: 1 addition & 1 deletion builtin/providers/azure/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (c *Config) Client() (interface{}, error) {
managementClient, err = management.NewClientFromConfig(
c.SubscriptionID,
c.ManagementCert,
management.ClientConfig{c.ManagementUrl},
management.ClientConfig{ManagementURL: c.ManagementUrl},
)
} else {
managementClient, err = management.NewClient(
Expand Down
48 changes: 30 additions & 18 deletions builtin/providers/azure/resource_azure_dns_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,26 @@ func resourceAzureDnsServerCreate(d *schema.ResourceData, meta interface{}) erro
log.Println("[DEBUG] Adding new DNS server definition to Azure.")
name := d.Get("name").(string)
address := d.Get("dns_address").(string)
netConf.Configuration.Dns.DnsServers = append(
netConf.Configuration.Dns.DnsServers,
virtualnetwork.DnsServer{
netConf.Configuration.DNS.DNSServers = append(
netConf.Configuration.DNS.DNSServers,
virtualnetwork.DNSServer{
Name: name,
IPAddress: address,
})

// send the configuration back to Azure:
log.Println("[INFO] Sending updated network configuration back to Azure.")
err = networkClient.SetVirtualNetworkConfiguration(netConf)
azureClient.mutex.Unlock()
reqID, err := networkClient.SetVirtualNetworkConfiguration(netConf)
if err != nil {
return fmt.Errorf("Failed issuing update to network configuration: %s", err)
}
err = managementClient.WaitForOperation(reqID, nil)
if err != nil {
return fmt.Errorf("Failed setting updated network configuration: %s", err)
return fmt.Errorf("Error setting network configuration: %s", err)
}

d.SetId(getRandomStringLabel(50))
azureClient.mutex.Unlock()
return nil
}

Expand All @@ -94,7 +98,7 @@ func resourceAzureDnsServerRead(d *schema.ResourceData, meta interface{}) error
name := d.Get("name").(string)

// search for our DNS and update it if the IP has been changed:
for _, dns := range netConf.Configuration.Dns.DnsServers {
for _, dns := range netConf.Configuration.DNS.DNSServers {
if dns.Name == name {
found = true
d.Set("dns_address", dns.IPAddress)
Expand Down Expand Up @@ -135,20 +139,24 @@ func resourceAzureDnsServerUpdate(d *schema.ResourceData, meta interface{}) erro
}

// search for our DNS and update its address value:
for i, dns := range netConf.Configuration.Dns.DnsServers {
for i, dns := range netConf.Configuration.DNS.DNSServers {
found = true
if dns.Name == name {
netConf.Configuration.Dns.DnsServers[i].IPAddress = d.Get("dns_address").(string)
netConf.Configuration.DNS.DNSServers[i].IPAddress = d.Get("dns_address").(string)
}
}

// if the config has changes, send the configuration back to Azure:
if found && caddress {
log.Println("[INFO] Sending updated network configuration back to Azure.")
err = networkClient.SetVirtualNetworkConfiguration(netConf)
reqID, err := networkClient.SetVirtualNetworkConfiguration(netConf)
if err != nil {
return fmt.Errorf("Failed issuing update to network configuration: %s", err)
}
err = managementClient.WaitForOperation(reqID, nil)
azureClient.mutex.Unlock()
if err != nil {
return fmt.Errorf("Failed setting updated network configuration: %s", err)
return fmt.Errorf("Error setting network configuration: %s", err)
}
}
}
Expand Down Expand Up @@ -180,7 +188,7 @@ func resourceAzureDnsServerExists(d *schema.ResourceData, meta interface{}) (boo
name := d.Get("name").(string)

// search for the DNS server's definition:
for _, dns := range netConf.Configuration.Dns.DnsServers {
for _, dns := range netConf.Configuration.DNS.DNSServers {
if dns.Name == name {
return true, nil
}
Expand Down Expand Up @@ -209,21 +217,25 @@ func resourceAzureDnsServerDelete(d *schema.ResourceData, meta interface{}) erro
name := d.Get("name").(string)

// search for the DNS server's definition and remove it:
for i, dns := range netConf.Configuration.Dns.DnsServers {
for i, dns := range netConf.Configuration.DNS.DNSServers {
if dns.Name == name {
netConf.Configuration.Dns.DnsServers = append(
netConf.Configuration.Dns.DnsServers[:i],
netConf.Configuration.Dns.DnsServers[i+1:]...,
netConf.Configuration.DNS.DNSServers = append(
netConf.Configuration.DNS.DNSServers[:i],
netConf.Configuration.DNS.DNSServers[i+1:]...,
)
}
}

// send the configuration back to Azure:
log.Println("[INFO] Sending updated network configuration back to Azure.")
err = networkClient.SetVirtualNetworkConfiguration(netConf)
reqID, err := networkClient.SetVirtualNetworkConfiguration(netConf)
if err != nil {
return fmt.Errorf("Failed issuing update to network configuration: %s", err)
}
err = managementClient.WaitForOperation(reqID, nil)
azureClient.mutex.Unlock()
if err != nil {
return fmt.Errorf("Failed setting updated network configuration: %s", err)
return fmt.Errorf("Error setting network configuration: %s", err)
}

d.SetId("")
Expand Down
40 changes: 21 additions & 19 deletions builtin/providers/azure/resource_azure_hosted_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"log"

"github.com/Azure/azure-sdk-for-go/management"
"github.com/Azure/azure-sdk-for-go/management/hostedservice"
"github.com/hashicorp/terraform/helper/schema"
)
Expand Down Expand Up @@ -89,24 +90,19 @@ func resourceAzureHostedServiceCreate(d *schema.ResourceData, meta interface{})
label := getRandomStringLabel(50)
d.SetId(label)

reqID, err := hostedServiceClient.CreateHostedService(
serviceName,
location,
reverseDNS,
label,
description,
err := hostedServiceClient.CreateHostedService(
hostedservice.CreateHostedServiceParameters{
ServiceName: serviceName,
Location: location,
Label: label,
Description: description,
ReverseDNSFqdn: reverseDNS,
},
)
if err != nil {
return fmt.Errorf("Failed defining new Azure hosted service: %s", err)
}

log.Println("[DEBUG] Waiting for hosted service creation.")
log.Println("[INFO] Creating new Azure hosted service.")
err = managementClient.WaitAsyncOperation(reqID)
if err != nil {
return fmt.Errorf("Failed creating new Azure hosted service: %s", err)
}

return nil
}

Expand All @@ -121,12 +117,18 @@ func resourceAzureHostedServiceExists(d *schema.ResourceData, meta interface{})

log.Println("[INFO] Querying for hosted service existence.")
serviceName := d.Get("service_name").(string)
exists, _, err := hostedServiceClient.CheckHostedServiceNameAvailability(serviceName)
_, err := hostedServiceClient.GetHostedService(serviceName)
if err != nil {
return false, fmt.Errorf("Failed to query for hosted service name availability: %s", err)
if management.IsResourceNotFoundError(err) {
// it means that the hosted service has been deleted in the meantime...
d.SetId("")
return false, nil
} else {
return false, fmt.Errorf("Failed to query for hosted service name availability: %s", err)
}
}

return exists, nil
return true, nil
}

// resourceAzureHostedServiceRead does all the necessary API calls
Expand All @@ -147,12 +149,12 @@ func resourceAzureHostedServiceRead(d *schema.ResourceData, meta interface{}) er

log.Println("[DEBUG] Reading hosted service query result data.")
d.Set("service_name", hostedService.ServiceName)
d.Set("url", hostedService.Url)
d.Set("url", hostedService.URL)
d.Set("location", hostedService.Location)
d.SetId(hostedService.Label)
d.Set("description", hostedService.Description)
d.Set("status", hostedService.Status)
d.Set("reverse_dns_fqdn", hostedService.ReverseDnsFqdn)
d.Set("reverse_dns_fqdn", hostedService.ReverseDNSFqdn)
d.Set("default_certificate_thumbprint", hostedService.DefaultWinRmCertificateThumbprint)

return nil
Expand Down Expand Up @@ -183,7 +185,7 @@ func resourceAzureHostedServiceDelete(d *schema.ResourceData, meta interface{})
}

log.Println("[DEBUG] Awaiting confirmation on hosted service deletion.")
err = managementClient.WaitAsyncOperation(reqID)
err = managementClient.WaitForOperation(reqID, nil)
if err != nil {
return fmt.Errorf("Error on hosted service deletion: %s", err)
}
Expand Down
56 changes: 29 additions & 27 deletions builtin/providers/azure/resource_azure_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"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/virtualmachine"
"github.com/Azure/azure-sdk-for-go/management/vmutils"
// "github.com/Azure/azure-sdk-for-go/management/vmutils"
"github.com/hashicorp/terraform/helper/schema"
)

Expand Down Expand Up @@ -153,7 +153,7 @@ func resourceAzureInstanceCreate(d *schema.ResourceData, meta interface{}) error
// general variables:
label := getRandomStringLabel(50)
d.SetId(label)
image := d.Get("image").(string)
// image := d.Get("image").(string)
name := d.Get("name").(string)
location := d.Get("location").(string)
description := d.Get("descriptiom").(string)
Expand All @@ -167,53 +167,55 @@ func resourceAzureInstanceCreate(d *schema.ResourceData, meta interface{}) error
if err != nil {
if serviceName == "" || management.IsResourceNotFoundError(err) {
log.Println("[INFO] No hosted service with the given name exists, creating new one with the instance's name.")
reqID, err := hostedServiceClient.CreateHostedService(
name,
location,
"",
label,
description,
err := hostedServiceClient.CreateHostedService(
hostedservice.CreateHostedServiceParameters{
ServiceName: name,
Location: location,
ReverseDNSFqdn: "",
Label: label,
Description: description,
},
)
if err != nil {
return fmt.Errorf("Error defining new Azure hosted service: %s", err)
}

err = managementClient.WaitAsyncOperation(reqID)
if err != nil {
return fmt.Errorf("Error creating new Azure hosted service: %s", err)
}
// err = managementClient.WaitForOperation(reqID, nil)
// if err != nil {
// return fmt.Errorf("Error creating new Azure hosted service: %s", err)
// }
} else {
fmt.Errorf("Error querying for existing hosted service.")
}
}

// create VM configuration:
role := vmutils.NewVmConfiguration(name, d.Get("size").(string))
// role := vmutils.NewVmConfiguration(name, d.Get("size").(string))

// configure the VM's storage:
// TODO(aznashwan): put things right here:
storAccount := d.Get("storage_account").(string)
storContainer := d.Get("storage_container").(string)
vhdURL := fmt.Sprintf("http://%s.blob.core.windows.net/%s/%s.vhd", storAccount, storContainer, name)
// storAccount := d.Get("storage_account").(string)
// storContainer := d.Get("storage_container").(string)
// vhdURL := fmt.Sprintf("http://%s.blob.core.windows.net/%s/%s.vhd", storAccount, storContainer, name)

err = vmutils.ConfigureDeploymentFromPlatformImage(&role, image, vhdURL, label)
if err != nil {
return fmt.Errorf("Failed to configure deployment: %s", err)
}
// err = vmutils.ConfigureDeploymentFromPlatformImage(&role, image, vhdURL, label)
// if err != nil {
// return fmt.Errorf("Failed to configure deployment: %s", err)
//}

// configure VM details:
userName := d.Get("user_name").(string)
userPass := d.Get("user_password").(string)
vmutils.ConfigureForLinux(&role, name, userName, userPass)
vmutils.ConfigureWithPublicSSH(&role)
//userName := d.Get("user_name").(string)
//userPass := d.Get("user_password").(string)
//vmutils.ConfigureForLinux(&role, name, userName, userPass)
//vmutils.ConfigureWithPublicSSH(&role)

// deploy the VM:
// reqID, err := virtualmachine.NewClient(managementClient).CreateDeployment(role, serviceName)
// if err != nil {
// return fmt.Errorf("Failed to initiate deployment creation: %s", err)
// }

// err = managementClient.WaitAsyncOperation(reqID)
// err = managementClient.WaitForOperation(reqID, nil)
// if err != nil {
// return fmt.Errorf("Deployment creation failed: ", err)
// }
Expand Down Expand Up @@ -244,7 +246,7 @@ func resourceAzureInstanceRead(d *schema.ResourceData, meta interface{}) error {
if role.InstanceName == name {
d.Set("status", role.InstanceStatus)
d.Set("power_state", role.PowerState)
d.Set("private_ip", role.IpAddress)
// d.Set("private_ip", role.IpAddress)
d.Set("host_name", role.HostName)
d.Set("agent_status", role.GuestAgentStatus)

Expand Down Expand Up @@ -284,7 +286,7 @@ func resourceAzureInstanceDelete(d *schema.ResourceData, meta interface{}) error
return fmt.Errorf("Failed to issue deployment deletion request: %s", err)
}

err = managementClient.WaitAsyncOperation(reqID)
err = managementClient.WaitForOperation(reqID, nil)
if err != nil {
return fmt.Errorf("Deployment deletion failed: %s", err)
}
Expand Down
22 changes: 17 additions & 5 deletions builtin/providers/azure/resource_azure_local_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,16 @@ func resourceAzureLocalNetworkConnectionCreate(d *schema.ResourceData, meta inte

// send the configuration back to Azure:
log.Println("[INFO] Sending updated network configuration back to Azure.")
err = networkClient.SetVirtualNetworkConfiguration(netConf)
azureClient.mutex.Unlock()
reqID, err := networkClient.SetVirtualNetworkConfiguration(netConf)
if err != nil {
return fmt.Errorf("Failed setting updated network configuration: %s", err)
}
err = managementClient.WaitForOperation(reqID, nil)
if err != nil {
return fmt.Errorf("Failed updating the network configuration: %s", err)
}

azureClient.mutex.Unlock()
d.SetId(getRandomStringLabel(50))
return nil
}
Expand Down Expand Up @@ -179,10 +183,14 @@ func resourceAzureLocalNetworkConnectionUpdate(d *schema.ResourceData, meta inte
} else if cvpn || cprefixes {
// else, send the configuration back to Azure:
log.Println("[INFO] Sending updated network configuration back to Azure.")
err = networkClient.SetVirtualNetworkConfiguration(netConf)
reqID, err := networkClient.SetVirtualNetworkConfiguration(netConf)
if err != nil {
return fmt.Errorf("Failed setting updated network configuration: %s", err)
}
err = managementClient.WaitForOperation(reqID, nil)
if err != nil {
return fmt.Errorf("Failed updating the network configuration: %s", err)
}
}

azureClient.mutex.Unlock()
Expand Down Expand Up @@ -247,12 +255,16 @@ func resourceAzureLocalNetworkConnectionDelete(d *schema.ResourceData, meta inte

// send the configuration back to Azure:
log.Println("[INFO] Sending updated network configuration back to Azure.")
err = networkClient.SetVirtualNetworkConfiguration(netConf)
azureClient.mutex.Unlock()
reqID, err := networkClient.SetVirtualNetworkConfiguration(netConf)
if err != nil {
return fmt.Errorf("Failed setting updated network configuration: %s", err)
}
err = managementClient.WaitForOperation(reqID, nil)
if err != nil {
return fmt.Errorf("Failed updating the network configuration: %s", err)
}

azureClient.mutex.Unlock()
d.SetId("")
return nil
}
4 changes: 2 additions & 2 deletions builtin/providers/azure/resource_azure_security_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func resourceAzureSecurityGroupCreate(d *schema.ResourceData, meta interface{})
return fmt.Errorf("Error whilst sending network security group create request to Azure: %s", err)
}

err = managementClient.WaitAsyncOperation(reqID)
err = managementClient.WaitForOperation(reqID, nil)
if err != nil {
return fmt.Errorf("Error creating network security group on Azure: %s", err)
}
Expand Down Expand Up @@ -126,7 +126,7 @@ func resourceAzureSecurityGroupDelete(d *schema.ResourceData, meta interface{})
if err != nil {
return fmt.Errorf("Error whilst issuing Azure network security group deletion: %s", err)
}
err = managementClient.WaitAsyncOperation(reqID)
err = managementClient.WaitForOperation(reqID, nil)
if err != nil {
return fmt.Errorf("Error in Azure network security group deletion: %s", err)
}
Expand Down
Loading

0 comments on commit a0a86ae

Please sign in to comment.