Skip to content

Commit

Permalink
Saving progress before adaptation.
Browse files Browse the repository at this point in the history
  • Loading branch information
aznashwan committed Jun 2, 2015
1 parent c1e7bff commit 9b97e32
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 156 deletions.
39 changes: 13 additions & 26 deletions builtin/providers/azure/resource_azure_dns_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,7 @@ func resourceAzureDnsServer() *schema.Resource {
// resourceAzureDnsServerCreate does all the necessary API calls
// to create a new DNS server definition on Azure.
func resourceAzureDnsServerCreate(d *schema.ResourceData, meta interface{}) error {
azureClient, ok := meta.(*AzureClient)
if !ok {
return fmt.Errorf("Failed to convert to *AzureClient, got: %T", meta)
}
azureClient := meta.(*AzureClient)
managementClient := azureClient.managementClient
networkClient := virtualnetwork.NewClient(managementClient)

Expand Down Expand Up @@ -81,10 +78,7 @@ func resourceAzureDnsServerCreate(d *schema.ResourceData, meta interface{}) erro
// resourceAzureDnsServerRead does all the necessary API calls to read
// the state of the DNS server off Azure.
func resourceAzureDnsServerRead(d *schema.ResourceData, meta interface{}) error {
azureClient, ok := meta.(*AzureClient)
if !ok {
return fmt.Errorf("Failed to convert to *AzureClient, got: %T", meta)
}
azureClient := meta.(*AzureClient)
managementClient := azureClient.managementClient
networkClient := virtualnetwork.NewClient(managementClient)

Expand All @@ -102,6 +96,7 @@ func resourceAzureDnsServerRead(d *schema.ResourceData, meta interface{}) error
if dns.Name == name {
found = true
d.Set("dns_address", dns.IPAddress)
break
}
}

Expand All @@ -116,39 +111,35 @@ func resourceAzureDnsServerRead(d *schema.ResourceData, meta interface{}) error
// resourceAzureDnsServerUpdate does all the necessary API calls
// to update the DNS definition on Azure.
func resourceAzureDnsServerUpdate(d *schema.ResourceData, meta interface{}) error {
azureClient, ok := meta.(*AzureClient)
if !ok {
return fmt.Errorf("Failed to convert to *AzureClient, got: %T", meta)
}
azureClient := meta.(*AzureClient)
managementClient := azureClient.managementClient
networkClient := virtualnetwork.NewClient(managementClient)

var err error
var found bool
name := d.Get("name").(string)
caddress := d.HasChange("dns_address")
var netConf virtualnetwork.NetworkConfiguration

if caddress {
log.Println("[DEBUG] DNS server address has changes; updating it on Azure.")
log.Println("[INFO] Fetching current network configuration from Azure.")
azureClient.mutex.Lock()
defer azureClient.mutex.Unlock()
netConf, err = networkClient.GetVirtualNetworkConfiguration()
netConf, err := networkClient.GetVirtualNetworkConfiguration()
if err != nil {
return fmt.Errorf("Failed to get the current network configuration from Azure: %s", err)
}

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

// if the config has changes, send the configuration back to Azure:
if found && caddress {
if found {
log.Println("[INFO] Sending updated network configuration back to Azure.")
reqID, err := networkClient.SetVirtualNetworkConfiguration(netConf)
if err != nil {
Expand All @@ -172,10 +163,7 @@ func resourceAzureDnsServerUpdate(d *schema.ResourceData, meta interface{}) erro
// resourceAzureDnsServerExists does all the necessary API calls to
// check if the DNS server definition alredy exists on Azure.
func resourceAzureDnsServerExists(d *schema.ResourceData, meta interface{}) (bool, error) {
azureClient, ok := meta.(*AzureClient)
if !ok {
return false, fmt.Errorf("Failed to convert to *AzureClient, got: %T", meta)
}
azureClient := meta.(*AzureClient)
managementClient := azureClient.managementClient
networkClient := virtualnetwork.NewClient(managementClient)

Expand All @@ -194,16 +182,15 @@ func resourceAzureDnsServerExists(d *schema.ResourceData, meta interface{}) (boo
}
}

// if we reached this point; the resource must have been deleted; and we must untrack it:
d.SetId("")
return false, nil
}

// resourceAzureDnsServerDelete does all the necessary API calls
// to delete the DNS server definition from Azure.
func resourceAzureDnsServerDelete(d *schema.ResourceData, meta interface{}) error {
azureClient, ok := meta.(*AzureClient)
if !ok {
return fmt.Errorf("Failed to convert to *AzureClient, got: %T", meta)
}
azureClient := meta.(*AzureClient)
managementClient := azureClient.managementClient
networkClient := virtualnetwork.NewClient(managementClient)

Expand All @@ -224,6 +211,7 @@ func resourceAzureDnsServerDelete(d *schema.ResourceData, meta interface{}) erro
netConf.Configuration.DNS.DNSServers[:i],
netConf.Configuration.DNS.DNSServers[i+1:]...,
)
break
}
}

Expand All @@ -238,6 +226,5 @@ func resourceAzureDnsServerDelete(d *schema.ResourceData, meta interface{}) erro
return fmt.Errorf("Error setting network configuration: %s", err)
}

d.SetId("")
return nil
}
29 changes: 12 additions & 17 deletions builtin/providers/azure/resource_azure_hosted_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,7 @@ func resourceAzureHostedService() *schema.Resource {
// resourceAzureHostedServiceCreate does all the necessary API calls
// to create a hosted service on Azure.
func resourceAzureHostedServiceCreate(d *schema.ResourceData, meta interface{}) error {
azureClient, ok := meta.(*AzureClient)
if !ok {
return fmt.Errorf("Failed to convert to *AzureClient, got: %T", meta)
}
azureClient := meta.(*AzureClient)
managementClient := azureClient.managementClient
hostedServiceClient := hostedservice.NewClient(managementClient)

Expand Down Expand Up @@ -109,10 +106,7 @@ func resourceAzureHostedServiceCreate(d *schema.ResourceData, meta interface{})
// resourceAzureHostedServiceExists checks whether a hosted service with the
// given service_name already exists on Azure.
func resourceAzureHostedServiceExists(d *schema.ResourceData, meta interface{}) (bool, error) {
azureClient, ok := meta.(*AzureClient)
if !ok {
return false, fmt.Errorf("Failed to convert to *AzureClient, got: %T", meta)
}
azureClient := meta.(*AzureClient)
hostedServiceClient := hostedservice.NewClient(azureClient.managementClient)

log.Println("[INFO] Querying for hosted service existence.")
Expand All @@ -134,17 +128,21 @@ func resourceAzureHostedServiceExists(d *schema.ResourceData, meta interface{})
// resourceAzureHostedServiceRead does all the necessary API calls
// to read the state of a hosted service from Azure.
func resourceAzureHostedServiceRead(d *schema.ResourceData, meta interface{}) error {
azureClient, ok := meta.(*AzureClient)
if !ok {
return fmt.Errorf("Failed to convert to *AzureClient, got: %T", meta)
}
azureClient := meta.(*AzureClient)
hostedServiceClient := hostedservice.NewClient(azureClient.managementClient)

log.Println("[INFO] Querying for hosted service info.")
serviceName := d.Get("service_name").(string)
hostedService, err := hostedServiceClient.GetHostedService(serviceName)
if err != nil {
return fmt.Errorf("Failed to get hosted service: %s", err)
if management.IsResourceNotFoundError(err) {
// it means the hosted service was deleted in the meantime,
// so we must remove it here:
d.SetId("")
return nil
} else {
return fmt.Errorf("Failed to get hosted service: %s", err)
}
}

log.Println("[DEBUG] Reading hosted service query result data.")
Expand All @@ -169,10 +167,7 @@ func resourceAzureHostedServiceUpdate(d *schema.ResourceData, meta interface{})
// resourceAzureHostedServiceDelete does all the necessary API calls to
// delete a hosted service from Azure.
func resourceAzureHostedServiceDelete(d *schema.ResourceData, meta interface{}) error {
azureClient, ok := meta.(*AzureClient)
if !ok {
return fmt.Errorf("Failed to convert to *AzureClient, got: %T", meta)
}
azureClient := meta.(*AzureClient)
managementClient := azureClient.managementClient
hostedServiceClient := hostedservice.NewClient(managementClient)

Expand Down
9 changes: 8 additions & 1 deletion builtin/providers/azure/resource_azure_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ func resourceAzureInstance() *schema.Resource {
ForceNew: true,
Description: parameterDescriptions["image"],
},
"deployment_slot": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
// TODO(aznashwan): make this used:
// - seperate config for Linux and Windows
// - Windows to join domain.
Expand Down Expand Up @@ -203,10 +207,13 @@ func resourceAzureInstanceCreate(d *schema.ResourceData, meta interface{}) error
vmutils.ConfigureWithPublicSSH(&role)

// deploy the VM:
slot := d.Get("deployment_slot").(string)
reqID, err := virtualmachine.NewClient(managementClient).CreateDeployment(
role,
serviceName,
virtualmachine.CreateDeploymentOptions{},
virtualmachine.CreateDeploymentOptions{
DeploymentSlot: slot,
},
)
if err != nil {
return fmt.Errorf("Failed to initiate deployment creation: %s", err)
Expand Down
74 changes: 27 additions & 47 deletions builtin/providers/azure/resource_azure_security_group_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,13 @@ func resourceAzureSecurityGroupRule() *schema.Resource {
// resourceAzureSecurityGroupRuleCreate does all the necessary API calls to
// create a new network security group rule on Azure.
func resourceAzureSecurityGroupRuleCreate(d *schema.ResourceData, meta interface{}) error {
azureClient, ok := meta.(*AzureClient)
if !ok {
return fmt.Errorf("Failed to convert to *AzureClient, got: %T", meta)
}
azureClient := meta.(*AzureClient)
managementClient := azureClient.managementClient
netSecClient := netsecgroup.NewClient(managementClient)

// create and configure the RuleResponse:
rule := netsecgroup.RuleRequest{
// TODO(aznashwan): security checks here:
Name: d.Get("name").(string),
Type: netsecgroup.RuleType(d.Get("type").(string)),
Priority: d.Get("priority").(int),
Expand Down Expand Up @@ -122,10 +120,7 @@ func resourceAzureSecurityGroupRuleCreate(d *schema.ResourceData, meta interface
// resourceAzureSecurityGroupRuleRead does all the necessary API calls to
// read the state of a network security group ruke off Azure.
func resourceAzureSecurityGroupRuleRead(d *schema.ResourceData, meta interface{}) error {
azureClient, ok := meta.(*AzureClient)
if !ok {
return fmt.Errorf("Failed to convert to *AzureClient, got: %T", meta)
}
azureClient := meta.(*AzureClient)
managementClient := azureClient.managementClient
netSecClient := netsecgroup.NewClient(managementClient)

Expand Down Expand Up @@ -161,6 +156,8 @@ func resourceAzureSecurityGroupRuleRead(d *schema.ResourceData, meta interface{}
d.Set("destination_address_prefix", rule.DestinationAddressPrefix)
d.Set("destination_port_range", rule.DestinationPortRange)
d.Set("protocol", rule.Protocol)

break
}
}

Expand All @@ -174,10 +171,7 @@ func resourceAzureSecurityGroupRuleRead(d *schema.ResourceData, meta interface{}
// resourceAzureSecurityGroupRuleUpdate does all the necessary API calls to
// update the state of a network security group ruke off Azure.
func resourceAzureSecurityGroupRuleUpdate(d *schema.ResourceData, meta interface{}) error {
azureClient, ok := meta.(*AzureClient)
if !ok {
return fmt.Errorf("Failed to convert to *AzureClient, got: %T", meta)
}
azureClient := meta.(*AzureClient)
managementClient := azureClient.managementClient
netSecClient := netsecgroup.NewClient(managementClient)

Expand Down Expand Up @@ -214,6 +208,7 @@ func resourceAzureSecurityGroupRuleUpdate(d *schema.ResourceData, meta interface

// else, start building up the rule request struct:
newRule := netsecgroup.RuleRequest{
// TODO(azhnashwan): Parameter check here:
Name: d.Get("name").(string),
Type: netsecgroup.RuleType(d.Get("type").(string)),
Priority: d.Get("priority").(int),
Expand Down Expand Up @@ -245,10 +240,7 @@ func resourceAzureSecurityGroupRuleUpdate(d *schema.ResourceData, meta interface
// resourceAzureSecurityGroupRuleExists does all the necessary API calls to
// check for the existence of the network security group rule on Azure.
func resourceAzureSecurityGroupRuleExists(d *schema.ResourceData, meta interface{}) (bool, error) {
azureClient, ok := meta.(*AzureClient)
if !ok {
return false, fmt.Errorf("Failed to convert to *AzureClient, got: %T", meta)
}
azureClient := meta.(*AzureClient)
managementClient := azureClient.managementClient
netSecClient := netsecgroup.NewClient(managementClient)

Expand All @@ -269,29 +261,24 @@ func resourceAzureSecurityGroupRuleExists(d *schema.ResourceData, meta interface
}

// try and find our security group rule:
var found bool
name := d.Get("name").(string)
for _, rule := range secgroup.Rules {
if rule.Name == name {
found = true
return true, nil
}
}
// check is the resource has not been deleted in the meantime:
if !found {
// if not; remove the resource:
d.SetId("")
}

return found, nil
// if here; it means the resource has been deleted in the
// meantime and must be removed from the schema:
d.SetId("")

return false, nil
}

// resourceAzureSecurityGroupRuleDelete does all the necessary API calls to
// delete a network security group rule off Azure.
func resourceAzureSecurityGroupRuleDelete(d *schema.ResourceData, meta interface{}) error {
azureClient, ok := meta.(*AzureClient)
if !ok {
return fmt.Errorf("Failed to convert to *AzureClient, got: %T", meta)
}
azureClient := meta.(*AzureClient)
managementClient := azureClient.managementClient
netSecClient := netsecgroup.NewClient(managementClient)

Expand All @@ -301,38 +288,31 @@ func resourceAzureSecurityGroupRuleDelete(d *schema.ResourceData, meta interface
log.Println("[INFO] Sending network security group rule query for deletion to Azure.")
secgroup, err := netSecClient.GetNetworkSecurityGroup(secGroupName)
if err != nil {
if !management.IsResourceNotFoundError(err) {
return fmt.Errorf("Error issuing network security group rules query: %s", err)
} else {
if management.IsResourceNotFoundError(err) {
// it meants that the network security group this rule belonged to has
// been deleted; so we need do nothing more but stop tracking the resource:
d.SetId("")
return nil
} else {
return fmt.Errorf("Error issuing network security group rules query: %s", err)
}
}

// check is the resource has not been deleted in the meantime:
var found bool
name := d.Get("name").(string)
for _, rule := range secgroup.Rules {
if rule.Name == name {
found = true
// if not; we shall issue the delete:
reqID, err := netSecClient.DeleteNetworkSecurityGroupRule(secGroupName, name)
if err != nil {
return fmt.Errorf("Error sending network security group rule delete request to Azure: %s", err)
}
err = managementClient.WaitForOperation(reqID, nil)
if err != nil {
return fmt.Errorf("Error deleting network security group rule off Azure: %s", err)
}
}
}
if !found {
// if not; remove the resource:
d.SetId("")
}

// if not; we shall issue the delete:
reqID, err := netSecClient.DeleteNetworkSecurityGroupRule(secGroupName, name)
if err != nil {
return fmt.Errorf("Error sending network security group rule delete request to Azure: %s", err)
}
err = managementClient.WaitForOperation(reqID, nil)
if err != nil {
return fmt.Errorf("Error deleting network security group rule off Azure: %s", err)
}

return nil
}
Loading

0 comments on commit 9b97e32

Please sign in to comment.