summaryrefslogtreecommitdiffabout
path: root/src/devmap.c
blob: 7cd63df809bc11004eddb2a08c8eea646b15edb3 (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
/* This file is part of Eclat.
   Copyright (C) 2013 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"

static int
isnumstr(const char *p)
{
	for (; *p; p++)
		if (!isdigit(*p))
			return 0;
	return 1;
}

void
eclat_encode_devmap(struct ec2_query *q, struct grecs_list *list)
{
	int i;
	char *bufptr = NULL;
	size_t bufsize = 0;
	struct grecs_list_entry *ep;

	if (!list)
		return;
	for (i = 1, ep = list->head; ep; ep = ep->next, i++) {
		char *dev = ep->data;
		char *p = strchr(dev, '=');

		if (!p)
			die(EX_USAGE, "malformed device mapping: %s", dev);
		*p++ = 0;

		grecs_asprintf(&bufptr, &bufsize,
			       "BlockDeviceMapping.%d.DeviceName",
			       i);
		eclat_query_add_param(q, bufptr, dev);
		
		if (strcmp(p, "none") == 0) {
			grecs_asprintf(&bufptr, &bufsize,
				       "BlockDeviceMapping.%d.NoDevice",
				       i);
			eclat_query_add_param(q, bufptr, NULL);
		} else if (strncmp(p, "ephemeral", 9) == 0) {
			grecs_asprintf(&bufptr, &bufsize,
				       "BlockDeviceMapping.%d.VirtualName",
				       i);
			eclat_query_add_param(q, bufptr, p);
		} else {
			struct wordsplit ws;

	/* [snapshot-id]:[volume-size]:[true|false]:[standard|io1[:iops]] */
			ws.ws_delim = ":";
			if (wordsplit(p, &ws,
				      WRDSF_NOVAR | WRDSF_NOCMD | WRDSF_DELIM))
				die(EX_SOFTWARE,
				    "failed to split string %s: %s",
				    p,
				    wordsplit_strerror(&ws));
			if (ws.ws_wordc == 1) {
				if (isnumstr(ws.ws_wordv[0])) {
					grecs_asprintf(&bufptr, &bufsize,
						       "BlockDeviceMapping.%d."
						       "Ebs.VolumeSize",
						       i);
				} else {
					translate_ids(1, &ws.ws_wordv[0],
						      MAP_SNAPSHOT);
					if (strncmp(ws.ws_wordv[0],
						    "snap-", 5) == 0) {
						grecs_asprintf(&bufptr,
							       &bufsize,
						       "BlockDeviceMapping.%d."
						       "Ebs.SnapshotId",
							       i);
					} else {
						die(EX_USAGE,
						    "unrecognized word \"%s\", "
						    "expected either a "
						    "snapshot ID, "
						    "or disk size, "
						    "or a device-specification",
						    ws.ws_wordv[0]);
					}
				}
				eclat_query_add_param(q, bufptr,
						      ws.ws_wordv[0]);
			} else if (ws.ws_wordc < 2) {
				die(EX_USAGE,
				    "not enough parts in device mapping \"%s\"",
				    p);		
			} else {
				if (ws.ws_wordv[0][0]) {
					grecs_asprintf(&bufptr, &bufsize,
						       "BlockDeviceMapping.%d."
						       "Ebs.SnapshotId",
						       i);
					eclat_query_add_param(q, bufptr,
							      ws.ws_wordv[0]);
				}
				
				if (ws.ws_wordv[1][0]) {
					grecs_asprintf(&bufptr, &bufsize,
						       "BlockDeviceMapping.%d."
						       "Ebs.VolumeSize",
						       i);
					eclat_query_add_param(q, bufptr,
							      ws.ws_wordv[1]);
				}
			}

			switch (ws.ws_wordc) {
			default:
				die(EX_USAGE,
				    "too many parts in device mapping \"%s\"",
				    p);		
			case 5:
				if (ws.ws_wordv[4][0]) {
					grecs_asprintf(&bufptr, &bufsize,
					       "BlockDeviceMapping.%d.Ebs.Iops",
						       i);
					eclat_query_add_param(q, bufptr, 
							      ws.ws_wordv[4]);
				}
				/* fall through */
			case 4:
				if (ws.ws_wordv[3][0]) {
					grecs_asprintf(&bufptr, &bufsize,
					 "BlockDeviceMapping.%d.Ebs.VolumeType",
						       i);
					eclat_query_add_param(q, bufptr,
							      ws.ws_wordv[3]);
				}
				/* fall through */
			case 3:
				if (ws.ws_wordv[2][0]) {
					if (strcmp(ws.ws_wordv[2], "false") &&
					    strcmp(ws.ws_wordv[2], "true"))
						die(EX_USAGE,
					    "expected \"true\" or \"false\", "
					    "but found \"%s\"",
						    ws.ws_wordv[2]);
					grecs_asprintf(&bufptr, &bufsize,
					       "BlockDeviceMapping.%d."
					       "Ebs.DeleteOnTermination",
						       i);
					eclat_query_add_param(q, bufptr,
							      ws.ws_wordv[2]);
				}
			case 2:
				break;
			}
			wordsplit_free(&ws);
		}		
	}
	free(bufptr);
}				       

Return to:

Send suggestions and report system problems to the System administrator.