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


Golang Set.Difference方法代碼示例

本文整理匯總了Golang中github.com/hashicorp/terraform/helper/schema.Set.Difference方法的典型用法代碼示例。如果您正苦於以下問題:Golang Set.Difference方法的具體用法?Golang Set.Difference怎麽用?Golang Set.Difference使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在github.com/hashicorp/terraform/helper/schema.Set的用法示例。


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

示例1: matchRules


//.........這裏部分代碼省略.........
					}
				}

				// match SGs. Both local and remote are already sets
				var localSGSet *schema.Set
				if lsRaw == nil {
					localSGSet = schema.NewSet(schema.HashString, nil)
				} else {
					localSGSet = lsRaw.(*schema.Set)
				}

				var remoteSGSet *schema.Set
				if rsRaw == nil {
					remoteSGSet = schema.NewSet(schema.HashString, nil)
				} else {
					remoteSGSet = rsRaw.(*schema.Set)
				}

				// Build up a list of local security groups that are found in the remote set
				for _, s := range localSGSet.List() {
					if remoteSGSet.Contains(s) {
						matchingSGs = append(matchingSGs, s.(string))
					}
				}

				// compare equalities for matches.
				// If we found the number of cidrs and number of sgs, we declare a
				// match, and then remove those elements from the remote rule, so that
				// this remote rule can still be considered by other local rules
				if numExpectedCidrs == len(matchingCidrs) {
					if numExpectedSGs == len(matchingSGs) {
						// confirm that self references match
						var lSelf bool
						var rSelf bool
						if _, ok := l["self"]; ok {
							lSelf = l["self"].(bool)
						}
						if _, ok := r["self"]; ok {
							rSelf = r["self"].(bool)
						}
						if rSelf == lSelf {
							delete(r, "self")
							// pop local cidrs from remote
							diffCidr := remoteCidrSet.Difference(localCidrSet)
							var newCidr []string
							for _, cRaw := range diffCidr.List() {
								newCidr = append(newCidr, cRaw.(string))
							}

							// reassigning
							if len(newCidr) > 0 {
								r["cidr_blocks"] = newCidr
							} else {
								delete(r, "cidr_blocks")
							}

							// pop local sgs from remote
							diffSGs := remoteSGSet.Difference(localSGSet)
							if len(diffSGs.List()) > 0 {
								r["security_groups"] = diffSGs
							} else {
								delete(r, "security_groups")
							}

							saves = append(saves, l)
						}
					}
				}
			}
		}
	}

	// Here we catch any remote rules that have not been stripped of all self,
	// cidrs, and security groups. We'll add remote rules here that have not been
	// matched locally, and let the graph sort things out. This will happen when
	// rules are added externally to Terraform
	for _, r := range remote {
		var lenCidr, lenSGs int
		if rCidrs, ok := r["cidr_blocks"]; ok {
			lenCidr = len(rCidrs.([]string))
		}

		if rawSGs, ok := r["security_groups"]; ok {
			lenSGs = len(rawSGs.(*schema.Set).List())
		}

		if _, ok := r["self"]; ok {
			if r["self"].(bool) == true {
				lenSGs++
			}
		}

		if lenSGs+lenCidr > 0 {
			log.Printf("[DEBUG] Found a remote Rule that wasn't empty: (%#v)", r)
			saves = append(saves, r)
		}
	}

	return saves
}
開發者ID:ephemeralsnow,項目名稱:terraform,代碼行數:101,代碼來源:resource_aws_security_group.go


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