summaryrefslogtreecommitdiff
path: root/vendor/github.com/mitchellh/packer/vendor/github.com/denverdino/aliyungo/ecs/route_tables.go
blob: cc85cb129ec8ed4f3e8af77e7ee3fe0b05141235 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package ecs

import (
	"time"

	"github.com/denverdino/aliyungo/common"
	"github.com/denverdino/aliyungo/util"
)

type DescribeRouteTablesArgs struct {
	VRouterId    string
	RouteTableId string
	common.Pagination
}

type RouteTableType string

const (
	RouteTableSystem = RouteTableType("System")
	RouteTableCustom = RouteTableType("Custom")
)

type RouteEntryStatus string

const (
	RouteEntryStatusPending   = RouteEntryStatus("Pending")
	RouteEntryStatusAvailable = RouteEntryStatus("Available")
	RouteEntryStatusModifying = RouteEntryStatus("Modifying")
)

type NextHopListType struct {
	NextHopList struct {
		NextHopItem []NextHopItemType
	}
}

type NextHopItemType struct {
	NextHopType string
	NextHopId   string
}

//
// You can read doc at http://docs.aliyun.com/#/pub/ecs/open-api/datatype&routeentrysettype
type RouteEntrySetType struct {
	RouteTableId         string
	DestinationCidrBlock string
	Type                 RouteTableType
	NextHopType          string
	NextHopId            string
	NextHopList          NextHopListType
	InstanceId           string
	Status               RouteEntryStatus // enum Pending | Available | Modifying
}

//
// You can read doc at http://docs.aliyun.com/#/pub/ecs/open-api/datatype&routetablesettype
type RouteTableSetType struct {
	VRouterId    string
	RouteTableId string
	RouteEntrys  struct {
		RouteEntry []RouteEntrySetType
	}
	RouteTableType RouteTableType
	CreationTime   util.ISO6801Time
}

type DescribeRouteTablesResponse struct {
	common.Response
	common.PaginationResult
	RouteTables struct {
		RouteTable []RouteTableSetType
	}
}

// DescribeRouteTables describes Virtual Routers
//
// You can read doc at http://docs.aliyun.com/#/pub/ecs/open-api/routertable&describeroutetables
func (client *Client) DescribeRouteTables(args *DescribeRouteTablesArgs) (routeTables []RouteTableSetType, pagination *common.PaginationResult, err error) {
	response, err := client.DescribeRouteTablesWithRaw(args)
	if err == nil {
		return response.RouteTables.RouteTable, &response.PaginationResult, nil
	}

	return nil, nil, err
}

func (client *Client) DescribeRouteTablesWithRaw(args *DescribeRouteTablesArgs) (response *DescribeRouteTablesResponse, err error) {
	args.Validate()
	response = &DescribeRouteTablesResponse{}

	err = client.Invoke("DescribeRouteTables", args, &response)

	if err == nil {
		return response, nil
	}

	return nil, err
}

type NextHopType string

const (
	NextHopIntance = NextHopType("Instance") //Default
	NextHopTunnel  = NextHopType("Tunnel")
)

type CreateRouteEntryArgs struct {
	RouteTableId         string
	DestinationCidrBlock string
	NextHopType          NextHopType
	NextHopId            string
	ClientToken          string
}

type CreateRouteEntryResponse struct {
	common.Response
}

// CreateRouteEntry creates route entry
//
// You can read doc at http://docs.aliyun.com/#/pub/ecs/open-api/routertable&createrouteentry
func (client *Client) CreateRouteEntry(args *CreateRouteEntryArgs) error {
	response := CreateRouteEntryResponse{}
	return client.Invoke("CreateRouteEntry", args, &response)
}

type DeleteRouteEntryArgs struct {
	RouteTableId         string
	DestinationCidrBlock string
	NextHopId            string
}

type DeleteRouteEntryResponse struct {
	common.Response
}

// DeleteRouteEntry deletes route entry
//
// You can read doc at http://docs.aliyun.com/#/pub/ecs/open-api/routertable&deleterouteentry
func (client *Client) DeleteRouteEntry(args *DeleteRouteEntryArgs) error {
	response := DeleteRouteEntryResponse{}
	return client.Invoke("DeleteRouteEntry", args, &response)
}

// WaitForAllRouteEntriesAvailable waits for all route entries to Available status
func (client *Client) WaitForAllRouteEntriesAvailable(vrouterId string, routeTableId string, timeout int) error {
	if timeout <= 0 {
		timeout = DefaultTimeout
	}
	args := DescribeRouteTablesArgs{
		VRouterId:    vrouterId,
		RouteTableId: routeTableId,
	}
	for {

		routeTables, _, err := client.DescribeRouteTables(&args)

		if err != nil {
			return err
		}
		if len(routeTables) == 0 {
			return common.GetClientErrorFromString("Not found")
		}
		success := true

	loop:
		for _, routeTable := range routeTables {
			for _, routeEntry := range routeTable.RouteEntrys.RouteEntry {
				if routeEntry.Status != RouteEntryStatusAvailable {
					success = false
					break loop
				}
			}
		}
		if success {
			break
		}
		timeout = timeout - DefaultWaitForInterval
		if timeout <= 0 {
			return common.GetClientErrorFromString("Timeout")
		}
		time.Sleep(DefaultWaitForInterval * time.Second)
	}
	return nil
}