func getInstanceAccessAddresses(d *schema.ResourceData, networks []map[string]interface{}) (string, string) { var hostv4, hostv6 string // Start with a global floating IP floatingIP := d.Get("floating_ip").(string) if floatingIP != "" { hostv4 = floatingIP } // Loop through all networks and check for the following: // * If the network is set as an access network. // * If the network has a floating IP. // * If the network has a v4/v6 fixed IP. for _, n := range networks { if n["floating_ip"] != nil { hostv4 = n["floating_ip"].(string) } else { if hostv4 == "" && n["fixed_ip_v4"] != nil { hostv4 = n["fixed_ip_v4"].(string) } } if hostv6 == "" && n["fixed_ip_v6"] != nil { hostv6 = n["fixed_ip_v6"].(string) } if an, ok := n["access_network"].(bool); ok && an { break } } log.Printf("[DEBUG] OpenStack Instance Network Access Addresses: %s, %s", hostv4, hostv6) return hostv4, hostv6 }
func resourceAwsIamAccessKeyCreate(d *schema.ResourceData, meta interface{}) error { iamconn := meta.(*AWSClient).iamconn request := &iam.CreateAccessKeyInput{ UserName: aws.String(d.Get("user").(string)), } createResp, err := iamconn.CreateAccessKey(request) if err != nil { return fmt.Errorf( "Error creating access key for user %s: %s", *request.UserName, err, ) } if err := d.Set("secret", createResp.AccessKey.SecretAccessKey); err != nil { return err } return resourceAwsIamAccessKeyReadResult(d, &iam.AccessKeyMetadata{ AccessKeyID: createResp.AccessKey.AccessKeyID, CreateDate: createResp.AccessKey.CreateDate, Status: createResp.AccessKey.Status, UserName: createResp.AccessKey.UserName, }) }
func associateFloatingIPsToInstance(computeClient *gophercloud.ServiceClient, d *schema.ResourceData) error { floatingIP := d.Get("floating_ip").(string) rawNetworks := d.Get("network").([]interface{}) instanceID := d.Id() if floatingIP != "" { if err := associateFloatingIPToInstance(computeClient, floatingIP, instanceID, ""); err != nil { return err } } else { for _, raw := range rawNetworks { if raw == nil { continue } rawMap := raw.(map[string]interface{}) if rawMap["floating_ip"].(string) != "" { floatingIP := rawMap["floating_ip"].(string) fixedIP := rawMap["fixed_ip_v4"].(string) if err := associateFloatingIPToInstance(computeClient, floatingIP, instanceID, fixedIP); err != nil { return err } } } } return nil }
func resourceAwsKinesisStreamDelete(d *schema.ResourceData, meta interface{}) error { conn := meta.(*AWSClient).kinesisconn sn := d.Get("name").(string) _, err := conn.DeleteStream(&kinesis.DeleteStreamInput{ StreamName: aws.String(sn), }) if err != nil { return err } stateConf := &resource.StateChangeConf{ Pending: []string{"DELETING"}, Target: "DESTROYED", Refresh: streamStateRefreshFunc(conn, sn), Timeout: 5 * time.Minute, Delay: 10 * time.Second, MinTimeout: 3 * time.Second, } _, err = stateConf.WaitForState() if err != nil { return fmt.Errorf( "Error waiting for Stream (%s) to be destroyed: %s", sn, err) } d.SetId("") return nil }
func resourceDigitalOceanFloatingIpCreate(d *schema.ResourceData, meta interface{}) error { client := meta.(*godo.Client) log.Printf("[INFO] Create a FloatingIP In a Region") regionOpts := &godo.FloatingIPCreateRequest{ Region: d.Get("region").(string), } log.Printf("[DEBUG] FloatingIP Create: %#v", regionOpts) floatingIp, _, err := client.FloatingIPs.Create(regionOpts) if err != nil { return fmt.Errorf("Error creating FloatingIP: %s", err) } d.SetId(floatingIp.IP) if v, ok := d.GetOk("droplet_id"); ok { log.Printf("[INFO] Assigning the Floating IP to the Droplet %d", v.(int)) action, _, err := client.FloatingIPActions.Assign(d.Id(), v.(int)) if err != nil { return fmt.Errorf( "Error Assigning FloatingIP (%s) to the droplet: %s", d.Id(), err) } _, unassignedErr := waitForFloatingIPReady(d, "completed", []string{"new", "in-progress"}, "status", meta, action.ID) if unassignedErr != nil { return fmt.Errorf( "Error waiting for FloatingIP (%s) to be Assigned: %s", d.Id(), unassignedErr) } } return resourceDigitalOceanFloatingIpRead(d, meta) }
func resourceSqlDatabaseCreate(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) database_name := d.Get("name").(string) instance_name := d.Get("instance").(string) project := config.Project db := &sqladmin.Database{ Name: database_name, Instance: instance_name, } op, err := config.clientSqlAdmin.Databases.Insert(project, instance_name, db).Do() if err != nil { return fmt.Errorf("Error, failed to insert "+ "database %s into instance %s: %s", database_name, instance_name, err) } err = sqladminOperationWait(config, op, "Insert Database") if err != nil { return fmt.Errorf("Error, failure waiting for insertion of %s "+ "into %s: %s", database_name, instance_name, err) } return resourceSqlDatabaseRead(d, meta) }
func resourceAwsIamAccessKeyRead(d *schema.ResourceData, meta interface{}) error { iamconn := meta.(*AWSClient).iamconn request := &iam.ListAccessKeysInput{ UserName: aws.String(d.Get("user").(string)), } getResp, err := iamconn.ListAccessKeys(request) if err != nil { if iamerr, ok := err.(awserr.Error); ok && iamerr.Code() == "NoSuchEntity" { // XXX TEST ME // the user does not exist, so the key can't exist. d.SetId("") return nil } return fmt.Errorf("Error reading IAM acces key: %s", err) } for _, key := range getResp.AccessKeyMetadata { if key.AccessKeyID != nil && *key.AccessKeyID == d.Id() { return resourceAwsIamAccessKeyReadResult(d, key) } } // Guess the key isn't around anymore. d.SetId("") return nil }
func resourceVSphereVirtualDiskDelete(d *schema.ResourceData, meta interface{}) error { client := meta.(*govmomi.Client) vDisk := virtualDisk{} if v, ok := d.GetOk("vmdk_path"); ok { vDisk.vmdkPath = v.(string) } if v, ok := d.GetOk("datastore"); ok { vDisk.datastore = v.(string) } dc, err := getDatacenter(client, d.Get("datacenter").(string)) if err != nil { return err } diskPath := fmt.Sprintf("[%v] %v", vDisk.datastore, vDisk.vmdkPath) virtualDiskManager := object.NewVirtualDiskManager(client.Client) task, err := virtualDiskManager.DeleteVirtualDisk(context.TODO(), diskPath, dc) if err != nil { return err } _, err = task.WaitForResult(context.TODO(), nil) if err != nil { log.Printf("[INFO] Failed to delete disk: %v", err) return err } log.Printf("[INFO] Deleted disk: %v", diskPath) d.SetId("") return nil }
func resourceComputeInstanceV2Delete(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) computeClient, err := config.computeV2Client(d.Get("region").(string)) if err != nil { return fmt.Errorf("Error creating OpenStack compute client: %s", err) } err = servers.Delete(computeClient, d.Id()).ExtractErr() if err != nil { return fmt.Errorf("Error deleting OpenStack server: %s", err) } // Wait for the instance to delete before moving on. log.Printf("[DEBUG] Waiting for instance (%s) to delete", d.Id()) stateConf := &resource.StateChangeConf{ Pending: []string{"ACTIVE"}, Target: []string{"DELETED"}, Refresh: ServerV2StateRefreshFunc(computeClient, d.Id()), Timeout: 30 * time.Minute, Delay: 10 * time.Second, MinTimeout: 3 * time.Second, } _, err = stateConf.WaitForState() if err != nil { return fmt.Errorf( "Error waiting for instance (%s) to delete: %s", d.Id(), err) } d.SetId("") return nil }
func getImageIDFromConfig(computeClient *gophercloud.ServiceClient, d *schema.ResourceData) (string, error) { // If block_device was used, an Image does not need to be specified. // If an Image was specified, ignore it if _, ok := d.GetOk("block_device"); ok { return "", nil } if imageId := d.Get("image_id").(string); imageId != "" { return imageId, nil } else { // try the OS_IMAGE_ID environment variable if v := os.Getenv("OS_IMAGE_ID"); v != "" { return v, nil } } imageName := d.Get("image_name").(string) if imageName == "" { // try the OS_IMAGE_NAME environment variable if v := os.Getenv("OS_IMAGE_NAME"); v != "" { imageName = v } } if imageName != "" { imageId, err := images.IDFromName(computeClient, imageName) if err != nil { return "", err } return imageId, nil } return "", fmt.Errorf("Neither a boot device, image ID, or image name were able to be determined.") }
func getNetworkName(d *schema.ResourceData, meta interface{}, networkID string) (string, error) { config := meta.(*Config) networkClient, err := config.networkingV2Client(d.Get("region").(string)) if err != nil { return "", fmt.Errorf("Error creating OpenStack network client: %s", err) } opts := networks.ListOpts{ID: networkID} pager := networks.List(networkClient, opts) networkName := "" err = pager.EachPage(func(page pagination.Page) (bool, error) { networkList, err := networks.ExtractNetworks(page) if err != nil { return false, err } for _, n := range networkList { if n.ID == networkID { networkName = n.Name return false, nil } } return true, nil }) return networkName, err }
func resourceAwsS3BucketLoggingUpdate(s3conn *s3.S3, d *schema.ResourceData) error { logging := d.Get("logging").(*schema.Set).List() bucket := d.Get("bucket").(string) loggingStatus := &s3.BucketLoggingStatus{} if len(logging) > 0 { c := logging[0].(map[string]interface{}) loggingEnabled := &s3.LoggingEnabled{} if val, ok := c["target_bucket"]; ok { loggingEnabled.TargetBucket = aws.String(val.(string)) } if val, ok := c["target_prefix"]; ok { loggingEnabled.TargetPrefix = aws.String(val.(string)) } loggingStatus.LoggingEnabled = loggingEnabled } i := &s3.PutBucketLoggingInput{ Bucket: aws.String(bucket), BucketLoggingStatus: loggingStatus, } log.Printf("[DEBUG] S3 put bucket logging: %#v", i) _, err := s3conn.PutBucketLogging(i) if err != nil { return fmt.Errorf("Error putting S3 logging: %s", err) } return nil }
func resourceAwsS3BucketVersioningUpdate(s3conn *s3.S3, d *schema.ResourceData) error { v := d.Get("versioning").(*schema.Set).List() bucket := d.Get("bucket").(string) vc := &s3.VersioningConfiguration{} if len(v) > 0 { c := v[0].(map[string]interface{}) if c["enabled"].(bool) { vc.Status = aws.String(s3.BucketVersioningStatusEnabled) } else { vc.Status = aws.String(s3.BucketVersioningStatusSuspended) } } else { vc.Status = aws.String(s3.BucketVersioningStatusSuspended) } i := &s3.PutBucketVersioningInput{ Bucket: aws.String(bucket), VersioningConfiguration: vc, } log.Printf("[DEBUG] S3 put bucket versioning: %#v", i) _, err := s3conn.PutBucketVersioning(i) if err != nil { return fmt.Errorf("Error putting S3 versioning: %s", err) } return nil }
func websiteEndpoint(s3conn *s3.S3, d *schema.ResourceData) (*S3Website, error) { // If the bucket doesn't have a website configuration, return an empty // endpoint if _, ok := d.GetOk("website"); !ok { return nil, nil } bucket := d.Get("bucket").(string) // Lookup the region for this bucket location, err := s3conn.GetBucketLocation( &s3.GetBucketLocationInput{ Bucket: aws.String(bucket), }, ) if err != nil { return nil, err } var region string if location.LocationConstraint != nil { region = *location.LocationConstraint } return WebsiteEndpoint(bucket, region), nil }
func resourceCloudStackInstanceRead(d *schema.ResourceData, meta interface{}) error { cs := meta.(*cloudstack.CloudStackClient) // Get the virtual machine details vm, count, err := cs.VirtualMachine.GetVirtualMachineByID(d.Id()) if err != nil { if count == 0 { log.Printf("[DEBUG] Instance %s does no longer exist", d.Get("name").(string)) d.SetId("") return nil } return err } // Update the config d.Set("name", vm.Name) d.Set("display_name", vm.Displayname) d.Set("ipaddress", vm.Nic[0].Ipaddress) //NB cloudstack sometimes sends back the wrong keypair name, so dont update it setValueOrID(d, "network", vm.Nic[0].Networkname, vm.Nic[0].Networkid) setValueOrID(d, "service_offering", vm.Serviceofferingname, vm.Serviceofferingid) setValueOrID(d, "template", vm.Templatename, vm.Templateid) setValueOrID(d, "project", vm.Project, vm.Projectid) setValueOrID(d, "zone", vm.Zonename, vm.Zoneid) return nil }
func resourceComputeServerGroupV2Read(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) computeClient, err := config.computeV2Client(d.Get("region").(string)) if err != nil { return fmt.Errorf("Error creating OpenStack compute client: %s", err) } sg, err := servergroups.Get(computeClient, d.Id()).Extract() if err != nil { return CheckDeleted(d, err, "server group") } log.Printf("[DEBUG] Retrieved ServerGroup %s: %+v", d.Id(), sg) // Set the name d.Set("name", sg.Name) // Set the policies policies := []string{} for _, p := range sg.Policies { policies = append(policies, p) } d.Set("policies", policies) // Set the members members := []string{} for _, m := range sg.Members { members = append(members, m) } d.Set("members", members) return nil }
func resourceAwsApiGatewayMethodDelete(d *schema.ResourceData, meta interface{}) error { conn := meta.(*AWSClient).apigateway log.Printf("[DEBUG] Deleting API Gateway Method: %s", d.Id()) return resource.Retry(5*time.Minute, func() *resource.RetryError { _, err := conn.DeleteMethod(&apigateway.DeleteMethodInput{ HttpMethod: aws.String(d.Get("http_method").(string)), ResourceId: aws.String(d.Get("resource_id").(string)), RestApiId: aws.String(d.Get("rest_api_id").(string)), }) if err == nil { return nil } apigatewayErr, ok := err.(awserr.Error) if apigatewayErr.Code() == "NotFoundException" { return nil } if !ok { return resource.NonRetryableError(err) } return resource.NonRetryableError(err) }) }
func resourceAwsS3BucketPolicyPut(d *schema.ResourceData, meta interface{}) error { s3conn := meta.(*AWSClient).s3conn bucket := d.Get("bucket").(string) policy := d.Get("policy").(string) d.SetId(bucket) log.Printf("[DEBUG] S3 bucket: %s, put policy: %s", bucket, policy) params := &s3.PutBucketPolicyInput{ Bucket: aws.String(bucket), Policy: aws.String(policy), } err := resource.Retry(1*time.Minute, func() *resource.RetryError { if _, err := s3conn.PutBucketPolicy(params); err != nil { if awserr, ok := err.(awserr.Error); ok { if awserr.Code() == "MalformedPolicy" { return resource.RetryableError(awserr) } } return resource.NonRetryableError(err) } return nil }) if err != nil { return fmt.Errorf("Error putting S3 policy: %s", err) } return nil }
func resourceInstanceMetadataV2(d *schema.ResourceData) map[string]string { m := make(map[string]string) for key, val := range d.Get("metadata").(map[string]interface{}) { m[key] = val.(string) } return m }
func resourceAwsKeyPairCreate(d *schema.ResourceData, meta interface{}) error { conn := meta.(*AWSClient).ec2conn var keyName string if v, ok := d.GetOk("key_name"); ok { keyName = v.(string) } else if v, ok := d.GetOk("key_name_prefix"); ok { keyName = resource.PrefixedUniqueId(v.(string)) } else { keyName = resource.UniqueId() } publicKey := d.Get("public_key").(string) req := &ec2.ImportKeyPairInput{ KeyName: aws.String(keyName), PublicKeyMaterial: []byte(publicKey), } resp, err := conn.ImportKeyPair(req) if err != nil { return fmt.Errorf("Error import KeyPair: %s", err) } d.SetId(*resp.KeyName) return nil }
func checkVolumeConfig(d *schema.ResourceData) error { // Although a volume_id is required to attach a volume, in order to be able to report // the attached volumes of an instance, it must be "computed" and thus "optional". // This accounts for situations such as "boot from volume" as well as volumes being // attached to the instance outside of Terraform. if v := d.Get("volume"); v != nil { vols := v.(*schema.Set).List() if len(vols) > 0 { for _, v := range vols { va := v.(map[string]interface{}) if va["volume_id"].(string) == "" { return fmt.Errorf("A volume_id must be specified when attaching volumes.") } } } } if vL, ok := d.GetOk("block_device"); ok { if len(vL.([]interface{})) > 1 { return fmt.Errorf("Can only specify one block device to boot from.") } } return nil }
// MapValueSpecs converts ResourceData into a map func MapValueSpecs(d *schema.ResourceData) map[string]string { m := make(map[string]string) for key, val := range d.Get("value_specs").(map[string]interface{}) { m[key] = val.(string) } return m }
func expandAzureRmVirtualMachineDataDisk(d *schema.ResourceData) ([]compute.DataDisk, error) { disks := d.Get("storage_data_disk").([]interface{}) data_disks := make([]compute.DataDisk, 0, len(disks)) for _, disk_config := range disks { config := disk_config.(map[string]interface{}) name := config["name"].(string) vhd := config["vhd_uri"].(string) createOption := config["create_option"].(string) lun := int32(config["lun"].(int)) disk_size := int32(config["disk_size_gb"].(int)) data_disk := compute.DataDisk{ Name: &name, Vhd: &compute.VirtualHardDisk{ URI: &vhd, }, Lun: &lun, DiskSizeGB: &disk_size, CreateOption: compute.DiskCreateOptionTypes(createOption), } data_disks = append(data_disks, data_disk) } return data_disks, nil }
func resourceSqlDatabaseDelete(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) database_name := d.Get("name").(string) instance_name := d.Get("instance").(string) project := config.Project op, err := config.clientSqlAdmin.Databases.Delete(project, instance_name, database_name).Do() if err != nil { return fmt.Errorf("Error, failed to delete"+ "database %s in instance %s: %s", database_name, instance_name, err) } err = sqladminOperationWait(config, op, "Delete Database") if err != nil { return fmt.Errorf("Error, failure waiting for deletion of %s "+ "in %s: %s", database_name, instance_name, err) } return nil }
func resourceComputeDiskDelete(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) // Delete the disk op, err := config.clientCompute.Disks.Delete( config.Project, d.Get("zone").(string), d.Id()).Do() if err != nil { return fmt.Errorf("Error deleting disk: %s", err) } // Wait for the operation to complete w := &OperationWaiter{ Service: config.clientCompute, Op: op, Project: config.Project, Zone: d.Get("zone").(string), Type: OperationWaitZone, } state := w.Conf() state.Timeout = 2 * time.Minute state.MinTimeout = 1 * time.Second opRaw, err := state.WaitForState() if err != nil { return fmt.Errorf("Error waiting for disk to delete: %s", err) } op = opRaw.(*compute.Operation) if op.Error != nil { // Return the error return OperationError(*op.Error) } d.SetId("") return nil }
func expandAzureRmVirtualMachineOsProfileLinuxConfig(d *schema.ResourceData) (*compute.LinuxConfiguration, error) { osProfilesLinuxConfig := d.Get("os_profile_linux_config").(*schema.Set).List() linuxConfig := osProfilesLinuxConfig[0].(map[string]interface{}) disablePasswordAuth := linuxConfig["disable_password_authentication"].(bool) config := &compute.LinuxConfiguration{ DisablePasswordAuthentication: &disablePasswordAuth, } linuxKeys := linuxConfig["ssh_keys"].([]interface{}) sshPublicKeys := []compute.SSHPublicKey{} for _, key := range linuxKeys { sshKey, ok := key.(map[string]interface{}) if !ok { continue } path := sshKey["path"].(string) keyData := sshKey["key_data"].(string) sshPublicKey := compute.SSHPublicKey{ Path: &path, KeyData: &keyData, } sshPublicKeys = append(sshPublicKeys, sshPublicKey) } config.SSH = &compute.SSHConfiguration{ PublicKeys: &sshPublicKeys, } return config, nil }
// 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) { secGroupClient := meta.(*Client).secGroupClient secGroupName := d.Get("security_group_name").(string) // get info on the network security group and search for our rule: log.Println("[INFO] Sending network security group rule query for existence check to Azure.") secgroup, err := secGroupClient.GetNetworkSecurityGroup(secGroupName) if err != nil { if !management.IsResourceNotFoundError(err) { return false, fmt.Errorf("Error issuing network security group rules query: %s", err) } else { // it meants that the network security group this rule belonged to has // been deleted; so we must remove this resource from the schema: d.SetId("") return false, nil } } // try and find our security group rule: name := d.Get("name").(string) for _, rule := range secgroup.Rules { if rule.Name == name { return true, 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 }
func resourceComputeTargetPoolRead(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) region, err := getRegion(d, config) if err != nil { return err } project, err := getProject(d, config) if err != nil { return err } tpool, err := config.clientCompute.TargetPools.Get( project, region, d.Id()).Do() if err != nil { if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 { log.Printf("[WARN] Removing Target Pool %q because it's gone", d.Get("name").(string)) // The resource doesn't exist anymore d.SetId("") return nil } return fmt.Errorf("Error reading TargetPool: %s", err) } d.Set("self_link", tpool.SelfLink) return nil }
func resourceListenerV2Read(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) networkingClient, err := config.networkingV2Client(d.Get("region").(string)) if err != nil { return fmt.Errorf("Error creating OpenStack networking client: %s", err) } listener, err := listeners.Get(networkingClient, d.Id()).Extract() if err != nil { return CheckDeleted(d, err, "LBV2 listener") } log.Printf("[DEBUG] Retrieved OpenStack LBaaSV2 listener %s: %+v", d.Id(), listener) d.Set("id", listener.ID) d.Set("name", listener.Name) d.Set("protocol", listener.Protocol) d.Set("tenant_id", listener.TenantID) d.Set("description", listener.Description) d.Set("protocol_port", listener.ProtocolPort) d.Set("admin_state_up", listener.AdminStateUp) d.Set("default_pool_id", listener.DefaultPoolID) d.Set("connection_limit", listener.ConnLimit) d.Set("sni_container_refs", listener.SniContainerRefs) d.Set("default_tls_container_ref", listener.DefaultTlsContainerRef) return nil }
func resourceLBMemberV1Delete(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) networkingClient, err := config.networkingV2Client(d.Get("region").(string)) if err != nil { return fmt.Errorf("Error creating OpenStack networking client: %s", err) } err = members.Delete(networkingClient, d.Id()).ExtractErr() if err != nil { CheckDeleted(d, err, "LB member") } stateConf := &resource.StateChangeConf{ Pending: []string{"ACTIVE", "PENDING_DELETE"}, Target: []string{"DELETED"}, Refresh: waitForLBMemberDelete(networkingClient, d.Id()), Timeout: 2 * time.Minute, Delay: 5 * time.Second, MinTimeout: 3 * time.Second, } _, err = stateConf.WaitForState() if err != nil { return fmt.Errorf("Error deleting OpenStack LB member: %s", err) } d.SetId("") return nil }