Esempio n. 1
0
func parse_aws_instance(rs *terraform.ResourceState) (*InstanceInfo, error) {
	info := InstanceInfo{}

	provider := aws.Provider().(*schema.Provider)
	instanceSchema := provider.ResourcesMap["aws_instance"].Schema
	stateReader := &schema.MapFieldReader{
		Schema: instanceSchema,
		Map:    schema.BasicMapReader(rs.Primary.Attributes),
	}

	// nameResult, err := stateReader.ReadField([]string{"id"})
	// if err != nil {
	// 	return nil, fmt.Errorf("Unable to read id field: %s", err)
	// }
	// info.Name = nameResult.ValueOrZero(instanceSchema["id"]).(string)
	info.Name = "moose"

	accessResult, err := stateReader.ReadField([]string{"public_ip"})
	if err != nil {
		return nil, fmt.Errorf("Unable to read a public_ip field: %s", err)
	}
	info.Address = accessResult.ValueOrZero(instanceSchema["public_ip"]).(string)
	if info.Address == "" {
		accessResult, err := stateReader.ReadField([]string{"private_ip"})
		if err != nil {
			return nil, fmt.Errorf("Unable to read a private_ip field: %s", err)
		}
		info.Address = accessResult.ValueOrZero(instanceSchema["private_ip"]).(string)
	}

	return &info, nil
}
func readV0Keys(
	is *terraform.InstanceState,
	res *schema.Resource,
) (*schema.Set, error) {
	reader := &schema.MapFieldReader{
		Schema: res.Schema,
		Map:    schema.BasicMapReader(is.Attributes),
	}
	result, err := reader.ReadField([]string{"key"})
	if err != nil {
		return nil, err
	}

	oldKeys, ok := result.Value.(*schema.Set)
	if !ok {
		return nil, fmt.Errorf("Got unexpected value from state: %#v", result.Value)
	}
	return oldKeys, nil
}
Esempio n. 3
0
// Function parse_os_compute_instance_v2 uses terraform routines to
// parse info out of a terraform.ResourceState.
//
// HEADS UP: it's use of these routines is slightly underhanded (but
// better than reverse engineering the state file format...).
//
// Thanks to @apparentlymart for this bit of code.
// See: https://github.com/hashicorp/terraform/issues/3405
func parse_os_compute_instance_v2(rs *terraform.ResourceState) (*InstanceInfo, error) {
	info := InstanceInfo{}

	provider := openstack.Provider().(*schema.Provider)
	instanceSchema := provider.ResourcesMap["openstack_compute_instance_v2"].Schema
	stateReader := &schema.MapFieldReader{
		Schema: instanceSchema,
		Map:    schema.BasicMapReader(rs.Primary.Attributes),
	}

	metadataResult, err := stateReader.ReadField([]string{"metadata"})
	if err != nil {
		return nil, errors.New("Unable to read metadata from ResourceState")
	}
	m := metadataResult.ValueOrZero(instanceSchema["metadata"])
	for key, value := range m.(map[string]interface{}) {
		if key == "ansible_groups" {
			groups := splitOnComma(value.(string))
			info.Groups = append(info.Groups, groups...)
		} else if key == "ansible_hostvars" {
			info.HostVars, err = parseVars(value.(string))
			if err != nil {
				return nil, fmt.Errorf("Unable to parse host variables: %s", err)
			}
		}
	}

	nameResult, err := stateReader.ReadField([]string{"name"})
	if err != nil {
		return nil, fmt.Errorf("Unable to read name field: %s", err)
	}
	info.Name = nameResult.ValueOrZero(instanceSchema["name"]).(string)

	accessResult, err := stateReader.ReadField([]string{"access_ip_v4"})
	if err != nil {
		return nil, fmt.Errorf("Unable to read access_ip_v4 field: %s", err)
	}
	info.Address = accessResult.ValueOrZero(instanceSchema["access_ip_v4"]).(string)

	return &info, nil
}
Esempio n. 4
0
func parse_digitalocean_droplet(rs *terraform.ResourceState) (*InstanceInfo, error) {
	info := InstanceInfo{}

	provider := digitalocean.Provider().(*schema.Provider)
	instanceSchema := provider.ResourcesMap["digitalocean_droplet"].Schema
	stateReader := &schema.MapFieldReader{
		Schema: instanceSchema,
		Map:    schema.BasicMapReader(rs.Primary.Attributes),
	}

	nameResult, err := stateReader.ReadField([]string{"name"})
	if err != nil {
		return nil, fmt.Errorf("Unable to read name field: %s", err)
	}
	info.Name = nameResult.ValueOrZero(instanceSchema["name"]).(string)

	accessResult, err := stateReader.ReadField([]string{"ipv4_address"})
	if err != nil {
		return nil, fmt.Errorf("Unable to read ipv4_address field: %s", err)
	}
	info.Address = accessResult.ValueOrZero(instanceSchema["ipv4_address"]).(string)

	return &info, nil
}