aboutsummaryrefslogtreecommitdiff
path: root/src/routetable.c
blob: 9b0c57776a9f95325c0ffc406801daee54ed4f86 (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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/* This file is part of Eclat.
   Copyright (C) 2012-2015 Sergey Poznyakoff.
 
   Eclat is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.
 
   Eclat is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
 
   You should have received a copy of the GNU General Public License
   along with Eclat.  If not, see <http://www.gnu.org/licenses/>. */

#include "eclat.h"

int
eclat_create_route_table(eclat_command_env_t *env, int argc, char **argv)
{
	int i;
	struct ec2_request *q = env->request;
	generic_proginfo->args_doc = "VPC-ID";
	generic_parse_options(env->cmd,
			      "create route table",
			      argc, argv, &i);
	argc -= i;
	argv += i;
	if (argc != 1)
		die(EX_USAGE, "wrong number of arguments");
	translate_ids(1, argv, MAP_VPC);
	eclat_request_add_param(q, "VpcId", argv[0]);
	return 0;
}

int
eclat_delete_route_table(eclat_command_env_t *env, int argc, char **argv)
{
	int i;
	struct ec2_request *q = env->request;

	generic_proginfo->args_doc = "RTAB-ID";
	generic_parse_options(env->cmd,
			      "delete route table",
			      argc, argv, &i);
	argv += i;
	argc -= i;

	if (argc != 1)
		die(EX_USAGE, "wrong number of arguments");

//	translate_ids(1, argv, MAP_ROUTE_TABLE);
	eclat_request_add_param(q, "RouteTableId", argv[0]);

	return 0;
}

static struct filter_descr rtab_filters[] = {
	{ "association.route-table-association-id", FILTER_STRING },
	{ "association.route-table-id", FILTER_STRING },
	{ "association.subnet-id", FILTER_STRING },
	{ "association.main", FILTER_BOOL },
	{ "route-table-id", FILTER_STRING },
	{ "route.destination-cidr-block", FILTER_STRING },
	{ "route.destination-prefix-list-id", FILTER_STRING },
	{ "route.gateway-id", FILTER_STRING },
	{ "route.instance-id", FILTER_STRING },
	{ "route.origin", FILTER_STRING },
	{ "route.state", FILTER_STRING },
	{ "route.vpc-peering-connection-id", FILTER_STRING },
	{ "tag-key", FILTER_STRING },
	{ "tag-value", FILTER_STRING },
	{ "tag:<KEY>", FILTER_STRING },
	{ "vpc-id", FILTER_STRING },
	{ NULL }
};

int
eclat_describe_route_tables(eclat_command_env_t *env,
			    int argc, char **argv)
{
	int i;

	available_filters = rtab_filters;
	generic_proginfo->print_help_hook = list_filters;
	generic_proginfo->args_doc = "[FILTER...] [ID...]";
	generic_parse_options(env->cmd,
			      "describe route tables",
			      argc, argv, &i);
	argv += i;
	argc -= i;
	translate_ids(argc, argv, MAP_ROUTE_TABLE);

	describe_request_create(env, argc, argv, "RouteTableId");
	return 0;
}

int
eclat_associate_route_table(eclat_command_env_t *env,
			    int argc, char **argv)
{
	int i;
	struct ec2_request *q = env->request;

	generic_proginfo->args_doc = "RTAB-ID SUBNET-ID";
	generic_parse_options(env->cmd,
			      "associate a subnet with a route table",
			      argc, argv, &i);
	argv += i;
	argc -= i;
	if (argc != 2)
		die(EX_USAGE, "wrong number of arguments");
	
        translate_ids(1, argv, MAP_ROUTE_TABLE);
	translate_ids(1, argv+1, MAP_SUBNET);
	
	eclat_request_add_param(q, "RouteTableId", argv[0]);
	eclat_request_add_param(q, "SubnetId", argv[1]);

	return 0;
}

int
eclat_disassociate_route_table(eclat_command_env_t *env,
			       int argc, char **argv)
{
	int i;
	struct ec2_request *q = env->request;

	generic_proginfo->args_doc = "RTBASSOC-ID";
	generic_parse_options(env->cmd,
			      "disassociate a subnet from a route table",
			      argc, argv, &i);
	argv += i;
	argc -= i;
	if (argc != 1)
		die(EX_USAGE, "wrong number of arguments");
	eclat_request_add_param(q, "AssociationId", argv[0]);
	return 0;
}

static void
route_create_request(struct ec2_request *req, int argc, char **argv)
{
	char const *id;
	static struct idtab {
		char *name;
		char *id;
	} idtab[] = {
		{ "gw", "GatewayId" },
		{ "inst", "InstanceId" },
		{ "if", "NetworkInterfaceId" },
		{ "peer", "VpcPeeringConnectionId" },
		{ NULL }
	};
	struct idtab *p;
		
	if (argc != 2)
		die(EX_USAGE, "wrong number of arguments");
	id = argv[0];

	for (p = idtab; p->name; p++)
		if (strcmp (id, p->name) == 0) {
			id = p->id;
			break;
		}

	translate_ids(1, argv+1, id);
	eclat_request_add_param(req, id, argv[1]);
}

int
eclat_route(eclat_command_env_t *env, int argc, char **argv)
{
	int i;
	struct ec2_request *q = env->request;
	char *command;
	char *rtb_id;
	char *op;
	char *cidr;
	
	generic_proginfo->args_doc = "RTB-ID OP CIDR [DEST ID]";
	generic_parse_options(env->cmd,
			      "manipulate the route table",
			      argc, argv, &i);
	argv += i;
	argc -= i;

	if (argc < 3)
		die(EX_USAGE, "wrong number of arguments");

	translate_ids(1, argv, MAP_ROUTE_TABLE);

	rtb_id = argv[0];
	op = argv[1];
	cidr = argv[2];

	argc -= 3;
	argv += 3;
	
	if (strcmp(op, "add") == 0) {
		command = "CreateRoute";
		route_create_request(q, argc, argv);
	} else if (strcmp(op, "del") == 0) {
		command = "DeleteRoute";
		if (argc != 0)
			die(EX_USAGE, "wrong number of arguments");
	} else if (strcmp(op, "chg") == 0 || strcmp(op, "replace") == 0) {
		command = "ReplaceRoute";
		route_create_request(q, argc, argv);
	} else {
		die(EX_USAGE, "unrecognized operation %s", op);
	}

	eclat_request_add_param(q, "Action", command);
	eclat_request_add_param(q, "RouteTableId", rtb_id);
	eclat_request_add_param(q, "DestinationCidrBlock", cidr);
	return 0;
}

Return to:

Send suggestions and report system problems to the System administrator.