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


Golang EC2.DescribeSnapshots方法代碼示例

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


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

示例1: PurgeSnapshots

func PurgeSnapshots(svc *ec2.EC2) error {

	dsi := ec2.DescribeSnapshotsInput{}

	filter := &ec2.Filter{}
	filterName := "tag:" + PurgeAllowKey
	filter.Name = &filterName
	value := "true"
	filter.Values = []*string{&value}
	dsi.Filters = append(dsi.Filters, filter)

	dso, err := svc.DescribeSnapshots(&dsi)
	if err != nil {
		return fmt.Errorf("describeSnapshots error, %s", err)
	}

	purgeCount := 0
	for _, snapshot := range dso.Snapshots {
		var paVal string
		var found bool

		if paVal, found = getTagValue(PurgeAfterKey, snapshot.Tags); !found {
			log.Printf("snapshot ID %s has tag '%s' but does not have a '%s' tag. Skipping purge...", *snapshot.SnapshotId, PurgeAllowKey, PurgeAfterKey)
			continue
		}

		pa, err := time.Parse(PurgeAfterFormat, paVal)
		if err != nil {
			return err
		}

		if pa.Before(time.Now()) {
			deli := ec2.DeleteSnapshotInput{}
			deli.SnapshotId = snapshot.SnapshotId

			_, err := svc.DeleteSnapshot(&deli)
			if err != nil {
				return fmt.Errorf("error purging Snapshot ID %s, err %s", *snapshot.SnapshotId, err)
			}
			log.Printf("snapshot ID '%s' purged, size %d GiB\n", *snapshot.SnapshotId, *snapshot.VolumeSize)
			purgeCount++
		}
	}

	log.Printf("%d snapshots purged\n", purgeCount)

	return nil
}
開發者ID:porjo,項目名稱:auto-snapshot,代碼行數:48,代碼來源:main.go

示例2: deleteOldSnapshots

func deleteOldSnapshots(svc *ec2.EC2, days int) {
	output, err := svc.DescribeSnapshots(&ec2.DescribeSnapshotsInput{Filters: []*ec2.Filter{
		{
			Name: aws.String("description"),
			Values: []*string{
				aws.String(descriptionPrefix + "-*"),
			},
		},
	}})
	if err != nil {
		fmt.Printf("%T(%v)", err, err)
		os.Exit(-1)
	}
	time := time.Now().AddDate(0, 0, -1*days)
	for _, snapshot := range output.Snapshots {
		if snapshot.StartTime.Before(time) {
			svc.DeleteSnapshot(&ec2.DeleteSnapshotInput{SnapshotId: snapshot.SnapshotId})
		}
	}
}
開發者ID:csimsek,項目名稱:aws-autosnapshots-go,代碼行數:20,代碼來源:aws-autosnapshots.go


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