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


Golang config.Resource類代碼示例

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


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

示例1: resourceCountKeys

func (i *Interpolater) resourceCountKeys(
	ms *ModuleState,
	cr *config.Resource,
	v *config.ResourceVariable) ([]string, error) {
	id := v.ResourceId()

	// If we're NOT applying, then we assume we can read the count
	// from the state. Plan and so on may not have any state yet so
	// we do a full interpolation.
	if i.Operation != walkApply {
		count, err := cr.Count()
		if err != nil {
			return nil, err
		}

		result := make([]string, count)
		for i := 0; i < count; i++ {
			result[i] = fmt.Sprintf("%s.%d", id, i)
		}

		return result, nil
	}

	// We need to determine the list of resource keys to get values from.
	// This needs to be sorted so the order is deterministic. We used to
	// use "cr.Count()" but that doesn't work if the count is interpolated
	// and we can't guarantee that so we instead depend on the state.
	var resourceKeys []string
	for k, _ := range ms.Resources {
		// If we don't have the right prefix then ignore it
		if k != id && !strings.HasPrefix(k, id+".") {
			continue
		}

		// Add it to the list
		resourceKeys = append(resourceKeys, k)
	}
	sort.Strings(resourceKeys)
	return resourceKeys, nil
}
開發者ID:spotinst,項目名稱:terraform,代碼行數:40,代碼來源:interpolate.go

示例2: resourceCountMax

func (i *Interpolater) resourceCountMax(
	ms *ModuleState,
	cr *config.Resource,
	v *config.ResourceVariable) (int, error) {
	id := v.ResourceId()

	// If we're NOT applying, then we assume we can read the count
	// from the state. Plan and so on may not have any state yet so
	// we do a full interpolation.
	if i.Operation != walkApply {
		count, err := cr.Count()
		if err != nil {
			return 0, err
		}

		return count, nil
	}

	// We need to determine the list of resource keys to get values from.
	// This needs to be sorted so the order is deterministic. We used to
	// use "cr.Count()" but that doesn't work if the count is interpolated
	// and we can't guarantee that so we instead depend on the state.
	max := -1
	for k, _ := range ms.Resources {
		// Get the index number for this resource
		index := ""
		if k == id {
			// If the key is the id, then its just 0 (no explicit index)
			index = "0"
		} else if strings.HasPrefix(k, id+".") {
			// Grab the index number out of the state
			index = k[len(id+"."):]
			if idx := strings.IndexRune(index, '.'); idx >= 0 {
				index = index[:idx]
			}
		}

		// If there was no index then this resource didn't match
		// the one we're looking for, exit.
		if index == "" {
			continue
		}

		// Turn the index into an int
		raw, err := strconv.ParseInt(index, 0, 0)
		if err != nil {
			return 0, fmt.Errorf(
				"%s: error parsing index %q as int: %s",
				id, index, err)
		}

		// Keep track of this index if its the max
		if new := int(raw); new > max {
			max = new
		}
	}

	// If we never found any matching resources in the state, we
	// have zero.
	if max == -1 {
		return 0, nil
	}

	// The result value is "max+1" because we're returning the
	// max COUNT, not the max INDEX, and we zero-index.
	return max + 1, nil
}
開發者ID:hashicorp,項目名稱:terraform,代碼行數:67,代碼來源:interpolate.go


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