2016-03-21 16:49:48 +01:00
package azurerm
import (
"encoding/json"
"fmt"
"log"
"net/http"
2016-03-21 16:59:56 +01:00
"strings"
2016-03-21 16:49:48 +01:00
"time"
"github.com/Azure/azure-sdk-for-go/arm/resources/resources"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceArmTemplateDeployment ( ) * schema . Resource {
return & schema . Resource {
Create : resourceArmTemplateDeploymentCreate ,
Read : resourceArmTemplateDeploymentRead ,
Update : resourceArmTemplateDeploymentCreate ,
Delete : resourceArmTemplateDeploymentDelete ,
Schema : map [ string ] * schema . Schema {
"name" : & schema . Schema {
Type : schema . TypeString ,
Required : true ,
ForceNew : true ,
} ,
"resource_group_name" : & schema . Schema {
Type : schema . TypeString ,
Required : true ,
ForceNew : true ,
} ,
"template_body" : & schema . Schema {
Type : schema . TypeString ,
Optional : true ,
Computed : true ,
StateFunc : normalizeJson ,
} ,
"parameters" : & schema . Schema {
Type : schema . TypeMap ,
Optional : true ,
} ,
2016-03-21 16:59:56 +01:00
"outputs" : & schema . Schema {
Type : schema . TypeMap ,
Computed : true ,
} ,
2016-03-21 16:49:48 +01:00
"deployment_mode" : & schema . Schema {
Type : schema . TypeString ,
Required : true ,
} ,
} ,
}
}
func resourceArmTemplateDeploymentCreate ( d * schema . ResourceData , meta interface { } ) error {
client := meta . ( * ArmClient )
deployClient := client . deploymentsClient
name := d . Get ( "name" ) . ( string )
resGroup := d . Get ( "resource_group_name" ) . ( string )
2016-03-21 16:59:56 +01:00
deploymentMode := d . Get ( "deployment_mode" ) . ( string )
2016-03-21 16:49:48 +01:00
2016-03-21 16:59:56 +01:00
log . Printf ( "[INFO] preparing arguments for Azure ARM Template Deployment creation." )
2016-03-21 16:49:48 +01:00
properties := resources . DeploymentProperties {
2016-03-21 16:59:56 +01:00
Mode : resources . DeploymentMode ( deploymentMode ) ,
2016-03-21 16:49:48 +01:00
}
if v , ok := d . GetOk ( "parameters" ) ; ok {
params := v . ( map [ string ] interface { } )
2016-03-21 16:59:56 +01:00
newParams := make ( map [ string ] interface { } , len ( params ) )
for key , val := range params {
newParams [ key ] = struct {
Value interface { }
} {
Value : val ,
}
}
properties . Parameters = & newParams
2016-03-21 16:49:48 +01:00
}
if v , ok := d . GetOk ( "template_body" ) ; ok {
template , err := expandTemplateBody ( v . ( string ) )
if err != nil {
return err
}
properties . Template = & template
}
deployment := resources . Deployment {
Properties : & properties ,
}
resp , err := deployClient . CreateOrUpdate ( resGroup , name , deployment )
if err != nil {
return nil
}
d . SetId ( * resp . ID )
2016-03-21 16:59:56 +01:00
log . Printf ( "[DEBUG] Waiting for Template Deployment (%s) to become available" , name )
2016-03-21 16:49:48 +01:00
stateConf := & resource . StateChangeConf {
2016-03-21 16:59:56 +01:00
Pending : [ ] string { "creating" , "updating" , "accepted" , "running" } ,
Target : [ ] string { "succeeded" } ,
2016-03-21 16:49:48 +01:00
Refresh : templateDeploymentStateRefreshFunc ( client , resGroup , name ) ,
2016-04-23 22:41:20 +02:00
Timeout : 40 * time . Minute ,
2016-03-21 16:49:48 +01:00
}
if _ , err := stateConf . WaitForState ( ) ; err != nil {
return fmt . Errorf ( "Error waiting for Template Deployment (%s) to become available: %s" , name , err )
}
return resourceArmTemplateDeploymentRead ( d , meta )
}
func resourceArmTemplateDeploymentRead ( d * schema . ResourceData , meta interface { } ) error {
client := meta . ( * ArmClient )
deployClient := client . deploymentsClient
id , err := parseAzureResourceID ( d . Id ( ) )
if err != nil {
return err
}
resGroup := id . ResourceGroup
name := id . Path [ "deployments" ]
if name == "" {
name = id . Path [ "Deployments" ]
}
resp , err := deployClient . Get ( resGroup , name )
if resp . StatusCode == http . StatusNotFound {
d . SetId ( "" )
return nil
}
if err != nil {
2016-03-21 16:59:56 +01:00
return fmt . Errorf ( "Error making Read request on Azure RM Template Deployment %s: %s" , name , err )
}
var outputs map [ string ] string
if resp . Properties . Outputs != nil && len ( * resp . Properties . Outputs ) > 0 {
2016-03-21 20:40:00 +01:00
outputs = make ( map [ string ] string )
2016-03-21 16:59:56 +01:00
for key , output := range * resp . Properties . Outputs {
outputMap := output . ( map [ string ] interface { } )
outputValue , ok := outputMap [ "value" ]
if ! ok {
// No value
continue
}
outputs [ key ] = outputValue . ( string )
}
2016-03-21 16:49:48 +01:00
}
2016-03-21 20:40:00 +01:00
2016-03-21 16:59:56 +01:00
d . Set ( "outputs" , outputs )
2016-03-21 16:49:48 +01:00
return nil
}
func resourceArmTemplateDeploymentDelete ( d * schema . ResourceData , meta interface { } ) error {
client := meta . ( * ArmClient )
deployClient := client . deploymentsClient
id , err := parseAzureResourceID ( d . Id ( ) )
if err != nil {
return err
}
resGroup := id . ResourceGroup
name := id . Path [ "deployments" ]
if name == "" {
name = id . Path [ "Deployments" ]
}
_ , err = deployClient . Delete ( resGroup , name )
return nil
}
func expandTemplateBody ( template string ) ( map [ string ] interface { } , error ) {
var templateBody map [ string ] interface { }
err := json . Unmarshal ( [ ] byte ( template ) , & templateBody )
if err != nil {
2016-03-21 16:59:56 +01:00
return nil , fmt . Errorf ( "Error Expanding the template_body for Azure RM Template Deployment" )
2016-03-21 16:49:48 +01:00
}
return templateBody , nil
}
func normalizeJson ( jsonString interface { } ) string {
if jsonString == nil || jsonString == "" {
return ""
}
var j interface { }
err := json . Unmarshal ( [ ] byte ( jsonString . ( string ) ) , & j )
if err != nil {
return fmt . Sprintf ( "Error parsing JSON: %s" , err )
}
b , _ := json . Marshal ( j )
return string ( b [ : ] )
}
func templateDeploymentStateRefreshFunc ( client * ArmClient , resourceGroupName string , name string ) resource . StateRefreshFunc {
return func ( ) ( interface { } , string , error ) {
res , err := client . deploymentsClient . Get ( resourceGroupName , name )
if err != nil {
return nil , "" , fmt . Errorf ( "Error issuing read request in templateDeploymentStateRefreshFunc to Azure ARM for Template Deployment '%s' (RG: '%s'): %s" , name , resourceGroupName , err )
}
2016-03-21 16:59:56 +01:00
return res , strings . ToLower ( * res . Properties . ProvisioningState ) , nil
2016-03-21 16:49:48 +01:00
}
}