當前位置: 首頁>>代碼示例>>Golang>>正文


Golang flatmap.Flatten函數代碼示例

本文整理匯總了Golang中github.com/hashicorp/terraform/flatmap.Flatten函數的典型用法代碼示例。如果您正苦於以下問題:Golang Flatten函數的具體用法?Golang Flatten怎麽用?Golang Flatten使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了Flatten函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: testFlatAttrDiffs

// generate ResourceAttrDiffs for nested data structures in tests
func testFlatAttrDiffs(k string, i interface{}) map[string]*ResourceAttrDiff {
	diffs := make(map[string]*ResourceAttrDiff)
	// check for strings and empty containers first
	switch t := i.(type) {
	case string:
		diffs[k] = &ResourceAttrDiff{New: t}
		return diffs
	case map[string]interface{}:
		if len(t) == 0 {
			diffs[k] = &ResourceAttrDiff{New: ""}
			return diffs
		}
	case []interface{}:
		if len(t) == 0 {
			diffs[k] = &ResourceAttrDiff{New: ""}
			return diffs
		}
	}

	flat := flatmap.Flatten(map[string]interface{}{k: i})

	for k, v := range flat {
		attrDiff := &ResourceAttrDiff{
			Old: "",
			New: v,
		}
		diffs[k] = attrDiff
	}

	return diffs
}
開發者ID:paultyng,項目名稱:terraform,代碼行數:32,代碼來源:context_test.go

示例2: resource_consul_keys_diff

func resource_consul_keys_diff(
	s *terraform.ResourceState,
	c *terraform.ResourceConfig,
	meta interface{}) (*terraform.ResourceDiff, error) {

	// Determine the list of computed variables
	var computed []string
	keys, ok := flatmap.Expand(flatmap.Flatten(c.Config), "key").([]interface{})
	if !ok {
		goto AFTER
	}
	for _, sub := range keys {
		key, _, _, err := parse_key(sub)
		if err != nil {
			continue
		}
		computed = append(computed, "var."+key)
	}

AFTER:
	b := &diff.ResourceBuilder{
		Attrs: map[string]diff.AttrType{
			"datacenter": diff.AttrTypeCreate,
			"key":        diff.AttrTypeUpdate,
		},
		ComputedAttrsUpdate: computed,
	}
	return b.Diff(s, c)
}
開發者ID:GeorgeErickson,項目名稱:terraform-1,代碼行數:29,代碼來源:resource_consul_keys.go

示例3: resource_aws_db_instance_update_state

func resource_aws_db_instance_update_state(
	s *terraform.ResourceState,
	v *rds.DBInstance) (*terraform.ResourceState, error) {

	s.Attributes["address"] = v.Address
	s.Attributes["allocated_storage"] = strconv.Itoa(v.AllocatedStorage)
	s.Attributes["availability_zone"] = v.AvailabilityZone
	s.Attributes["backup_retention_period"] = strconv.Itoa(v.BackupRetentionPeriod)
	s.Attributes["backup_window"] = v.PreferredBackupWindow
	s.Attributes["endpoint"] = fmt.Sprintf("%s:%s", s.Attributes["address"], s.Attributes["port"])
	s.Attributes["engine"] = v.Engine
	s.Attributes["engine_version"] = v.EngineVersion
	s.Attributes["instance_class"] = v.DBInstanceClass
	s.Attributes["maintenance_window"] = v.PreferredMaintenanceWindow
	s.Attributes["multi_az"] = strconv.FormatBool(v.MultiAZ)
	s.Attributes["name"] = v.DBName
	s.Attributes["port"] = strconv.Itoa(v.Port)
	s.Attributes["status"] = v.DBInstanceStatus
	s.Attributes["username"] = v.MasterUsername

	// Flatten our group values
	toFlatten := make(map[string]interface{})

	if len(v.DBSecurityGroupNames) > 0 && v.DBSecurityGroupNames[0] != "" {
		toFlatten["security_group_names"] = v.DBSecurityGroupNames
	}
	if len(v.VpcSecurityGroupIds) > 0 && v.VpcSecurityGroupIds[0] != "" {
		toFlatten["vpc_security_group_ids"] = v.VpcSecurityGroupIds
	}
	for k, v := range flatmap.Flatten(toFlatten) {
		s.Attributes[k] = v
	}

	return s, nil
}
開發者ID:JasonGiedymin,項目名稱:terraform,代碼行數:35,代碼來源:resource_aws_db_instance.go

示例4: resource_aws_autoscaling_group_update_state

func resource_aws_autoscaling_group_update_state(
	s *terraform.ResourceState,
	g *autoscaling.AutoScalingGroup) (*terraform.ResourceState, error) {

	s.Attributes["min_size"] = strconv.Itoa(g.MinSize)
	s.Attributes["max_size"] = strconv.Itoa(g.MaxSize)
	s.Attributes["default_cooldown"] = strconv.Itoa(g.DefaultCooldown)
	s.Attributes["name"] = g.Name
	s.Attributes["desired_capacity"] = strconv.Itoa(g.DesiredCapacity)
	s.Attributes["health_check_grace_period"] = strconv.Itoa(g.HealthCheckGracePeriod)
	s.Attributes["health_check_type"] = g.HealthCheckType
	s.Attributes["launch_configuration"] = g.LaunchConfigurationName
	s.Attributes["vpc_zone_identifier"] = g.VPCZoneIdentifier

	// Flatten our group values
	toFlatten := make(map[string]interface{})

	// Special case the return of amazons load balancers names in the XML having
	// a blank entry
	if len(g.LoadBalancerNames) > 0 && g.LoadBalancerNames[0].LoadBalancerName != "" {
		toFlatten["load_balancers"] = flattenLoadBalancers(g.LoadBalancerNames)
	}

	toFlatten["availability_zones"] = flattenAvailabilityZones(g.AvailabilityZones)

	for k, v := range flatmap.Flatten(toFlatten) {
		s.Attributes[k] = v
	}

	return s, nil
}
開發者ID:kyriakosoikonomakos,項目名稱:terraform,代碼行數:31,代碼來源:resource_aws_autoscaling_group.go

示例5: resource_aws_elb_update_state

func resource_aws_elb_update_state(
	s *terraform.ResourceState,
	balancer *elb.LoadBalancer) (*terraform.ResourceState, error) {

	s.Attributes["name"] = balancer.LoadBalancerName
	s.Attributes["dns_name"] = balancer.DNSName

	// Flatten our group values
	toFlatten := make(map[string]interface{})

	if len(balancer.Instances) > 0 && balancer.Instances[0].InstanceId != "" {
		toFlatten["instances"] = flattenInstances(balancer.Instances)
	}

	// There's only one health check, so save that to state as we
	// currently can
	if balancer.HealthCheck.Target != "" {
		toFlatten["health_check"] = flattenHealthCheck(balancer.HealthCheck)
	}

	for k, v := range flatmap.Flatten(toFlatten) {
		s.Attributes[k] = v
	}

	return s, nil
}
開發者ID:WIZARD-CXY,項目名稱:golang-devops-stuff,代碼行數:26,代碼來源:resource_aws_elb.go

示例6: resource_heroku_app_update_state

func resource_heroku_app_update_state(
	s *terraform.ResourceState,
	app *application) (*terraform.ResourceState, error) {

	s.Attributes["name"] = app.App.Name
	s.Attributes["stack"] = app.App.Stack.Name
	s.Attributes["region"] = app.App.Region.Name
	s.Attributes["git_url"] = app.App.GitURL
	s.Attributes["web_url"] = app.App.WebURL

	// We know that the hostname on heroku will be the name+herokuapp.com
	// You need this to do things like create DNS CNAME records
	s.Attributes["heroku_hostname"] = fmt.Sprintf("%s.herokuapp.com", app.App.Name)

	toFlatten := make(map[string]interface{})

	if len(app.Vars) > 0 {
		toFlatten["config_vars"] = []map[string]string{app.Vars}
	}

	for k, v := range flatmap.Flatten(toFlatten) {
		s.Attributes[k] = v
	}

	return s, nil
}
開發者ID:JasonGiedymin,項目名稱:terraform,代碼行數:26,代碼來源:resource_heroku_app.go

示例7: resource_aws_security_group_update_state

func resource_aws_security_group_update_state(
	s *terraform.ResourceState,
	sg *ec2.SecurityGroupInfo) (*terraform.ResourceState, error) {

	s.Attributes["description"] = sg.Description
	s.Attributes["name"] = sg.Name
	s.Attributes["vpc_id"] = sg.VpcId
	s.Attributes["owner_id"] = sg.OwnerId

	// Flatten our ingress values
	toFlatten := make(map[string]interface{})
	toFlatten["ingress"] = flattenIPPerms(sg.IPPerms)

	for k, v := range flatmap.Flatten(toFlatten) {
		s.Attributes[k] = v
	}

	s.Dependencies = nil
	if s.Attributes["vpc_id"] != "" {
		s.Dependencies = append(s.Dependencies,
			terraform.ResourceDependency{ID: s.Attributes["vpc_id"]},
		)
	}

	return s, nil
}
開發者ID:routelastresort,項目名稱:terraform,代碼行數:26,代碼來源:resource_aws_security_group.go

示例8: resource_aws_instance_update_state

func resource_aws_instance_update_state(
	s *terraform.ResourceState,
	instance *ec2.Instance) (*terraform.ResourceState, error) {
	s.Attributes["availability_zone"] = instance.AvailZone
	s.Attributes["key_name"] = instance.KeyName
	s.Attributes["public_dns"] = instance.DNSName
	s.Attributes["public_ip"] = instance.PublicIpAddress
	s.Attributes["private_dns"] = instance.PrivateDNSName
	s.Attributes["private_ip"] = instance.PrivateIpAddress
	s.Attributes["subnet_id"] = instance.SubnetId
	s.Dependencies = nil

	// Extract the existing security groups
	useID := false
	if raw := flatmap.Expand(s.Attributes, "security_groups"); raw != nil {
		if sgs, ok := raw.([]interface{}); ok {
			for _, sg := range sgs {
				str, ok := sg.(string)
				if !ok {
					continue
				}

				if strings.HasPrefix(str, "sg-") {
					useID = true
					break
				}
			}
		}
	}

	// Build up the security groups
	sgs := make([]string, len(instance.SecurityGroups))
	for i, sg := range instance.SecurityGroups {
		if instance.SubnetId != "" && useID {
			sgs[i] = sg.Id
		} else {
			sgs[i] = sg.Name
		}

		s.Dependencies = append(s.Dependencies,
			terraform.ResourceDependency{ID: sg.Id},
		)
	}
	flatmap.Map(s.Attributes).Merge(flatmap.Flatten(map[string]interface{}{
		"security_groups": sgs,
	}))

	if instance.SubnetId != "" {
		s.Dependencies = append(s.Dependencies,
			terraform.ResourceDependency{ID: instance.SubnetId},
		)
	}

	return s, nil
}
開發者ID:JasonGiedymin,項目名稱:terraform,代碼行數:55,代碼來源:resource_aws_instance.go

示例9: resource_aws_security_group_update_state

func resource_aws_security_group_update_state(
	s *terraform.ResourceState,
	sg *ec2.SecurityGroupInfo) (*terraform.ResourceState, error) {

	s.Attributes["description"] = sg.Description
	s.Attributes["name"] = sg.Name
	s.Attributes["vpc_id"] = sg.VpcId
	s.Attributes["owner_id"] = sg.OwnerId

	// Flatten our ingress values
	toFlatten := make(map[string]interface{})

	ingressRules := make([]map[string]interface{}, 0, len(sg.IPPerms))
	for _, perm := range sg.IPPerms {
		n := make(map[string]interface{})
		n["from_port"] = perm.FromPort
		n["protocol"] = perm.Protocol
		n["to_port"] = perm.ToPort

		if len(perm.SourceIPs) > 0 {
			n["cidr_blocks"] = perm.SourceIPs
		}

		if len(perm.SourceGroups) > 0 {
			// We depend on other security groups
			for _, v := range perm.SourceGroups {
				s.Dependencies = append(s.Dependencies,
					terraform.ResourceDependency{ID: v.Id},
				)
			}
			n["security_groups"] = flattenSecurityGroups(perm.SourceGroups)
		}

		// Reverse the order, as Amazon sorts it the reverse of how we created
		// it.
		ingressRules = append([]map[string]interface{}{n}, ingressRules...)
	}

	toFlatten["ingress"] = ingressRules

	for k, v := range flatmap.Flatten(toFlatten) {
		s.Attributes[k] = v
	}

	if s.Attributes["vpc_id"] != "" {
		s.Dependencies = append(s.Dependencies,
			terraform.ResourceDependency{ID: s.Attributes["vpc_id"]},
		)
	}

	return s, nil
}
開發者ID:JasonGiedymin,項目名稱:terraform,代碼行數:52,代碼來源:resource_aws_security_group.go

示例10: resource_aws_r53_record_update_state

func resource_aws_r53_record_update_state(
	s *terraform.ResourceState,
	rec *route53.ResourceRecordSet) {

	flatRec := flatmap.Flatten(map[string]interface{}{
		"records": rec.Records,
	})
	for k, v := range flatRec {
		s.Attributes[k] = v
	}

	s.Attributes["ttl"] = strconv.FormatInt(int64(rec.TTL), 10)
}
開發者ID:nbrosnahan,項目名稱:terraform,代碼行數:13,代碼來源:resource_aws_route53_record.go

示例11: flattenActiveTrustedSigners

// Convert *cloudfront.ActiveTrustedSigners to a flatmap.Map type, which ensures
// it can probably be inserted into the schema.TypeMap type used by the
// active_trusted_signers attribute.
func flattenActiveTrustedSigners(ats *cloudfront.ActiveTrustedSigners) flatmap.Map {
	m := make(map[string]interface{})
	s := []interface{}{}
	m["enabled"] = *ats.Enabled

	for _, v := range ats.Items {
		signer := make(map[string]interface{})
		signer["aws_account_number"] = *v.AwsAccountNumber
		signer["key_pair_ids"] = aws.StringValueSlice(v.KeyPairIds.Items)
		s = append(s, signer)
	}
	m["items"] = s
	return flatmap.Flatten(m)
}
開發者ID:chandy,項目名稱:terraform,代碼行數:17,代碼來源:cloudfront_distribution_configuration_structure.go

示例12: Validate

func (v *Validator) Validate(
	c *terraform.ResourceConfig) (ws []string, es []error) {
	// Flatten the configuration so it is easier to reason about
	flat := flatmap.Flatten(c.Raw)

	keySet := make(map[string]validatorKey)
	for i, vs := range [][]string{v.Required, v.Optional} {
		req := i == 0
		for _, k := range vs {
			vk, err := newValidatorKey(k, req)
			if err != nil {
				es = append(es, err)
				continue
			}

			keySet[k] = vk
		}
	}

	purged := make([]string, 0)
	for _, kv := range keySet {
		p, w, e := kv.Validate(flat)
		if len(w) > 0 {
			ws = append(ws, w...)
		}
		if len(e) > 0 {
			es = append(es, e...)
		}

		purged = append(purged, p...)
	}

	// Delete all the keys we processed in order to find
	// the unknown keys.
	for _, p := range purged {
		delete(flat, p)
	}

	// The rest are unknown
	for k, _ := range flat {
		es = append(es, fmt.Errorf("Unknown configuration: %s", k))
	}

	return
}
開發者ID:AssertSelenium,項目名稱:terraform,代碼行數:45,代碼來源:validator.go

示例13: resource_aws_elb_update_state

func resource_aws_elb_update_state(
	s *terraform.ResourceState,
	balancer *elb.LoadBalancer) (*terraform.ResourceState, error) {

	s.Attributes["name"] = balancer.LoadBalancerName
	s.Attributes["dns_name"] = balancer.DNSName

	// Flatten our group values
	toFlatten := make(map[string]interface{})

	if len(balancer.Instances) > 0 && balancer.Instances[0].InstanceId != "" {
		toFlatten["instances"] = flattenInstances(balancer.Instances)
		for k, v := range flatmap.Flatten(toFlatten) {
			s.Attributes[k] = v
		}
	}

	return s, nil
}
開發者ID:routelastresort,項目名稱:terraform,代碼行數:19,代碼來源:resource_aws_elb.go

示例14: DefaultsMap

// DefaultsMap returns a map of default values for this variable.
func (v *Variable) DefaultsMap() map[string]string {
	if v.Default == nil {
		return nil
	}

	n := fmt.Sprintf("var.%s", v.Name)
	switch v.Type() {
	case VariableTypeString:
		return map[string]string{n: v.Default.(string)}
	case VariableTypeMap:
		result := flatmap.Flatten(map[string]interface{}{
			n: v.Default.(map[string]string),
		})
		result[n] = v.Name

		return result
	default:
		return nil
	}
}
開發者ID:jcy1011,項目名稱:terraform,代碼行數:21,代碼來源:config.go

示例15: resource_aws_launch_configuration_update_state

func resource_aws_launch_configuration_update_state(
	s *terraform.ResourceState,
	lc *autoscaling.LaunchConfiguration) (*terraform.ResourceState, error) {

	s.Attributes["image_id"] = lc.ImageId
	s.Attributes["instance_type"] = lc.InstanceType
	s.Attributes["key_name"] = lc.KeyName
	s.Attributes["name"] = lc.Name

	// Flatten our group values
	toFlatten := make(map[string]interface{})

	if len(lc.SecurityGroups) > 0 && lc.SecurityGroups[0].SecurityGroup != "" {
		toFlatten["security_groups"] = flattenAutoscalingSecurityGroups(lc.SecurityGroups)
	}

	for k, v := range flatmap.Flatten(toFlatten) {
		s.Attributes[k] = v
	}

	return s, nil
}
開發者ID:EZTABLE,項目名稱:terraform,代碼行數:22,代碼來源:resource_aws_launch_configuration.go


注:本文中的github.com/hashicorp/terraform/flatmap.Flatten函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。