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


Golang terraform.ResourceDiff類代碼示例

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


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

示例1: testResourceDiffStr

func testResourceDiffStr(rd *terraform.ResourceDiff) string {
	var buf bytes.Buffer

	crud := "UPDATE"
	if rd.RequiresNew() {
		crud = "CREATE"
	}

	buf.WriteString(fmt.Sprintf(
		"%s\n",
		crud))

	keyLen := 0
	keys := make([]string, 0, len(rd.Attributes))
	for key, _ := range rd.Attributes {
		keys = append(keys, key)
		if len(key) > keyLen {
			keyLen = len(key)
		}
	}
	sort.Strings(keys)

	for _, attrK := range keys {
		attrDiff := rd.Attributes[attrK]

		v := attrDiff.New
		if attrDiff.NewComputed {
			v = "<computed>"
		}
		if attrDiff.NewRemoved {
			v = "<removed>"
		}

		newResource := ""
		if attrDiff.RequiresNew {
			newResource = " (forces new resource)"
		}

		inOut := "IN "
		if attrDiff.Type == terraform.DiffAttrOutput {
			inOut = "OUT"
		}

		buf.WriteString(fmt.Sprintf(
			"  %s %s:%s %#v => %#v%s\n",
			inOut,
			attrK,
			strings.Repeat(" ", keyLen-len(attrK)),
			attrDiff.Old,
			v,
			newResource))
	}

	return buf.String()
}
開發者ID:EZTABLE,項目名稱:terraform,代碼行數:55,代碼來源:diff_test.go

示例2: Apply

// Apply performs a create or update depending on the diff, and calls
// the proper function on the matching Resource.
func (m *Map) Apply(
	s *terraform.ResourceState,
	d *terraform.ResourceDiff,
	meta interface{}) (*terraform.ResourceState, error) {
	r, ok := m.Mapping[s.Type]
	if !ok {
		return nil, fmt.Errorf("Unknown resource type: %s", s.Type)
	}

	if d.Destroy || d.RequiresNew() {
		if s.ID != "" {
			// Destroy the resource if it is created
			err := r.Destroy(s, meta)
			if err != nil {
				return s, err
			}

			s.ID = ""
		}

		// If we're only destroying, and not creating, then return now.
		// Otherwise, we continue so that we can create a new resource.
		if !d.RequiresNew() {
			return nil, nil
		}
	}

	var result *terraform.ResourceState
	var err error
	if s.ID == "" {
		result, err = r.Create(s, d, meta)
	} else {
		if r.Update == nil {
			return s, fmt.Errorf(
				"Resource type '%s' doesn't support update",
				s.Type)
		}

		result, err = r.Update(s, d, meta)
	}
	if result != nil {
		if result.Attributes == nil {
			result.Attributes = make(map[string]string)
		}

		result.Attributes["id"] = result.ID
	}

	return result, err
}
開發者ID:JasonGiedymin,項目名稱:terraform,代碼行數:52,代碼來源:map.go

示例3: Apply

// Apply creates, updates, and/or deletes a resource.
func (r *Resource) Apply(
	s *terraform.ResourceState,
	d *terraform.ResourceDiff,
	meta interface{}) (*terraform.ResourceState, error) {
	data, err := schemaMap(r.Schema).Data(s, d)
	if err != nil {
		return s, err
	}

	if s == nil {
		// The Terraform API dictates that this should never happen, but
		// it doesn't hurt to be safe in this case.
		s = new(terraform.ResourceState)
	}

	if d.Destroy || d.RequiresNew() {
		if s.ID != "" {
			// Destroy the resource since it is created
			if err := r.Delete(data, meta); err != nil {
				return data.State(), err
			}

			// Make sure the ID is gone.
			data.SetId("")
		}

		// If we're only destroying, and not creating, then return
		// now since we're done!
		if !d.RequiresNew() {
			return nil, nil
		}
	}

	err = nil
	if data.Id() == "" {
		// We're creating, it is a new resource.
		err = r.Create(data, meta)
	} else {
		if r.Update == nil {
			return s, fmt.Errorf("%s doesn't support update", s.Type)
		}

		err = r.Update(data, meta)
	}

	return data.State(), err
}
開發者ID:GeorgeErickson,項目名稱:terraform-1,代碼行數:48,代碼來源:resource.go

示例4: Diff

// Diff returns the diff for a resource given the schema map,
// state, and configuration.
func (m schemaMap) Diff(
	s *terraform.ResourceState,
	c *terraform.ResourceConfig) (*terraform.ResourceDiff, error) {
	result := new(terraform.ResourceDiff)
	result.Attributes = make(map[string]*terraform.ResourceAttrDiff)

	d := &ResourceData{
		schema:  m,
		state:   s,
		config:  c,
		diffing: true,
	}

	for k, schema := range m {
		err := m.diff(k, schema, result, d)
		if err != nil {
			return nil, err
		}
	}

	// Remove any nil diffs just to keep things clean
	for k, v := range result.Attributes {
		if v == nil {
			delete(result.Attributes, k)
		}
	}

	// Go through and detect all of the ComputedWhens now that we've
	// finished the diff.
	// TODO

	if result.Empty() {
		// If we don't have any diff elements, just return nil
		return nil, nil
	}

	return result, nil
}
開發者ID:jharriman,項目名稱:terraform,代碼行數:40,代碼來源:schema.go

示例5: Diff

// Diff returns the diff for a resource given the schema map,
// state, and configuration.
func (m schemaMap) Diff(
	s *terraform.ResourceState,
	c *terraform.ResourceConfig) (*terraform.ResourceDiff, error) {
	result := new(terraform.ResourceDiff)
	result.Attributes = make(map[string]*terraform.ResourceAttrDiff)

	d := &ResourceData{
		schema:  m,
		state:   s,
		config:  c,
		diffing: true,
	}

	for k, schema := range m {
		err := m.diff(k, schema, result, d)
		if err != nil {
			return nil, err
		}
	}

	// If the diff requires a new resource, then we recompute the diff
	// so we have the complete new resource diff, and preserve the
	// RequiresNew fields where necessary so the user knows exactly what
	// caused that.
	if result.RequiresNew() {
		// Create the new diff
		result2 := new(terraform.ResourceDiff)
		result2.Attributes = make(map[string]*terraform.ResourceAttrDiff)

		// Reset the data to not contain state
		d.state = nil

		// Perform the diff again
		for k, schema := range m {
			err := m.diff(k, schema, result2, d)
			if err != nil {
				return nil, err
			}
		}

		// Force all the fields to not force a new since we know what we
		// want to force new.
		for k, attr := range result2.Attributes {
			if attr == nil {
				continue
			}

			if attr.RequiresNew {
				attr.RequiresNew = false
			}

			if s != nil {
				attr.Old = s.Attributes[k]
			}
		}

		// Now copy in all the requires new diffs...
		for k, attr := range result.Attributes {
			if attr == nil {
				continue
			}

			newAttr, ok := result2.Attributes[k]
			if !ok {
				newAttr = attr
			}

			if attr.RequiresNew {
				newAttr.RequiresNew = true
			}

			result2.Attributes[k] = newAttr
		}

		// And set the diff!
		result = result2
	}

	// Remove any nil diffs just to keep things clean
	for k, v := range result.Attributes {
		if v == nil {
			delete(result.Attributes, k)
		}
	}

	// Go through and detect all of the ComputedWhens now that we've
	// finished the diff.
	// TODO

	if result.Empty() {
		// If we don't have any diff elements, just return nil
		return nil, nil
	}

	return result, nil
}
開發者ID:GeorgeErickson,項目名稱:terraform-1,代碼行數:98,代碼來源:schema.go


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