Skip to content

Commit c60e4b4

Browse files
Likitha Shettyprachi
authored andcommitted
Adding support in AWSAPI for CloudStack API's that implement the tags feature.
Verified the output of, 1. ec2-create-tags for all 4 supported ec2 resources(image, instance, volume and snapshot) with and without tag-value. 2. ec2-delete-tags for all types of created tags. 3. ec2-describe-tags with and without all supported filter.
1 parent 9e5fb17 commit c60e4b4

12 files changed

Lines changed: 784 additions & 17 deletions

awsapi/src/com/cloud/bridge/service/EC2SoapServiceImpl.java

Lines changed: 166 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import com.cloud.bridge.service.core.ec2.EC2CreateImageResponse;
3434
import com.cloud.bridge.service.core.ec2.EC2CreateKeyPair;
3535
import com.cloud.bridge.service.core.ec2.EC2CreateVolume;
36+
import com.cloud.bridge.service.core.ec2.EC2Tags;
3637
import com.cloud.bridge.service.core.ec2.EC2DeleteKeyPair;
3738
import com.cloud.bridge.service.core.ec2.EC2DescribeAddresses;
3839
import com.cloud.bridge.service.core.ec2.EC2DescribeAddressesResponse;
@@ -46,10 +47,13 @@
4647
import com.cloud.bridge.service.core.ec2.EC2DescribeInstancesResponse;
4748
import com.cloud.bridge.service.core.ec2.EC2DescribeKeyPairs;
4849
import com.cloud.bridge.service.core.ec2.EC2DescribeKeyPairsResponse;
50+
import com.cloud.bridge.service.core.ec2.EC2ResourceTag;
4951
import com.cloud.bridge.service.core.ec2.EC2DescribeSecurityGroups;
5052
import com.cloud.bridge.service.core.ec2.EC2DescribeSecurityGroupsResponse;
5153
import com.cloud.bridge.service.core.ec2.EC2DescribeSnapshots;
5254
import com.cloud.bridge.service.core.ec2.EC2DescribeSnapshotsResponse;
55+
import com.cloud.bridge.service.core.ec2.EC2DescribeTags;
56+
import com.cloud.bridge.service.core.ec2.EC2DescribeTagsResponse;
5357
import com.cloud.bridge.service.core.ec2.EC2DescribeVolumes;
5458
import com.cloud.bridge.service.core.ec2.EC2DescribeVolumesResponse;
5559
import com.cloud.bridge.service.core.ec2.EC2DisassociateAddress;
@@ -69,6 +73,8 @@
6973
import com.cloud.bridge.service.core.ec2.EC2RebootInstances;
7074
import com.cloud.bridge.service.core.ec2.EC2RegisterImage;
7175
import com.cloud.bridge.service.core.ec2.EC2ReleaseAddress;
76+
import com.cloud.bridge.service.core.ec2.EC2TagKeyValue;
77+
import com.cloud.bridge.service.core.ec2.EC2TagTypeId;
7278
import com.cloud.bridge.service.core.ec2.EC2RunInstances;
7379
import com.cloud.bridge.service.core.ec2.EC2RunInstancesResponse;
7480
import com.cloud.bridge.service.core.ec2.EC2SSHKeyPair;
@@ -79,6 +85,7 @@
7985
import com.cloud.bridge.service.core.ec2.EC2StartInstancesResponse;
8086
import com.cloud.bridge.service.core.ec2.EC2StopInstances;
8187
import com.cloud.bridge.service.core.ec2.EC2StopInstancesResponse;
88+
import com.cloud.bridge.service.core.ec2.EC2TagsFilterSet;
8289
import com.cloud.bridge.service.core.ec2.EC2Volume;
8390
import com.cloud.bridge.service.core.ec2.EC2VolumeFilterSet;
8491
import com.cloud.bridge.service.exception.EC2ServiceException;
@@ -199,6 +206,89 @@ public CreateVolumeResponse createVolume(CreateVolume createVolume) {
199206
return toCreateVolumeResponse( engine.createVolume( request ));
200207
}
201208

209+
public CreateTagsResponse createTags(CreateTags createTags) {
210+
EC2Tags request = new EC2Tags();
211+
CreateTagsType ctt = createTags.getCreateTags();
212+
213+
ResourceIdSetType resourceIds = ctt.getResourcesSet();
214+
ResourceTagSetType resourceTags = ctt.getTagSet();
215+
request = toResourceTypeAndIds(resourceIds);
216+
//add resource tag's to the request
217+
if (resourceTags != null) {
218+
ResourceTagSetItemType[] items = resourceTags.getItem();
219+
if (items != null) {
220+
for( int i=0; i < items.length; i++ ) {
221+
EC2TagKeyValue param1 = new EC2TagKeyValue();
222+
param1.setKey(items[i].getKey());
223+
param1.setValue(items[i].getValue());
224+
request.addResourceTag(param1);
225+
}
226+
}
227+
}
228+
return toCreateTagsResponse( engine.modifyTags( request, "create"));
229+
}
230+
231+
public DeleteTagsResponse deleteTags(DeleteTags deleteTags) {
232+
EC2Tags request = new EC2Tags();
233+
DeleteTagsType dtt = deleteTags.getDeleteTags();
234+
235+
ResourceIdSetType resourceIds = dtt.getResourcesSet();
236+
DeleteTagsSetType resourceTags = dtt.getTagSet();
237+
request = toResourceTypeAndIds(resourceIds);
238+
//add resource tag's to the request
239+
if (resourceTags != null) {
240+
DeleteTagsSetItemType[] items = resourceTags.getItem();
241+
if (items != null) {
242+
for( int i=0; i < items.length; i++ ) {
243+
EC2TagKeyValue param1 = new EC2TagKeyValue();
244+
param1.setKey(items[i].getKey());
245+
if (items[i].getValue() != null)
246+
param1.setValue(items[i].getValue());
247+
request.addResourceTag(param1);
248+
}
249+
}
250+
}
251+
return toDeleteTagsResponse( engine.modifyTags( request, "delete"));
252+
}
253+
254+
private EC2Tags toResourceTypeAndIds(ResourceIdSetType resourceIds) {
255+
EC2Tags request = new EC2Tags();
256+
//add resource-type and resource-id's to the request
257+
if (resourceIds != null) {
258+
ResourceIdSetItemType[] items = resourceIds.getItem();
259+
List<String> resourceTypeList = new ArrayList<String>();
260+
if (items != null) {
261+
for( int i=0; i < items.length; i++ ) {
262+
String resourceType = items[i].getResourceId().split(":")[0];
263+
if (resourceTypeList.isEmpty())
264+
resourceTypeList.add(resourceType);
265+
else {
266+
Boolean existsInList = false;
267+
for (String addedResourceType : resourceTypeList) {
268+
if (addedResourceType.equalsIgnoreCase(resourceType)) {
269+
existsInList = true;
270+
break;
271+
}
272+
}
273+
if (!existsInList)
274+
resourceTypeList.add(resourceType);
275+
}
276+
}
277+
for (String resourceType : resourceTypeList){
278+
EC2TagTypeId param1 = new EC2TagTypeId();
279+
param1.setResourceType(resourceType);
280+
for( int i=0; i < items.length; i++ ) {
281+
String[] resourceTag = items[i].getResourceId().split(":");
282+
if (resourceType.equals(resourceTag[0]))
283+
param1.addResourceId(resourceTag[1]);
284+
}
285+
request.addResourceType(param1);
286+
}
287+
}
288+
}
289+
return request;
290+
}
291+
202292
public DeleteSecurityGroupResponse deleteSecurityGroup(DeleteSecurityGroup deleteSecurityGroup) {
203293
DeleteSecurityGroupType sgt = deleteSecurityGroup.getDeleteSecurityGroup();
204294
return toDeleteSecurityGroupResponse( engine.deleteSecurityGroup( sgt.getGroupName()));
@@ -434,7 +524,18 @@ public DescribeSnapshotsResponse describeSnapshots(DescribeSnapshots describeSna
434524
return toDescribeSnapshotsResponse(engine.handleRequest(request));
435525
}
436526

437-
527+
public DescribeTagsResponse describeTags(DescribeTags decsribeTags) {
528+
EC2DescribeTags request = new EC2DescribeTags();
529+
DescribeTagsType dtt = decsribeTags.getDescribeTags();
530+
531+
FilterSetType fst = dtt.getFilterSet();
532+
533+
if (fst != null)
534+
request.setFilterSet( toTagsFilterSet( fst ));
535+
536+
return toDescribeTagsResponse(engine.describeTags(request));
537+
}
538+
438539
public DescribeVolumesResponse describeVolumes(DescribeVolumes describeVolumes)
439540
{
440541
EC2DescribeVolumes request = new EC2DescribeVolumes();
@@ -1094,8 +1195,28 @@ private EC2AvailabilityZonesFilterSet toAvailabiltyZonesFilterSet( FilterSetType
10941195
}
10951196
return azfs;
10961197
}
1097-
1098-
1198+
1199+
private EC2TagsFilterSet toTagsFilterSet( FilterSetType fst ) {
1200+
EC2TagsFilterSet tfs = new EC2TagsFilterSet();
1201+
1202+
FilterType[] items = fst.getItem();
1203+
if (items != null) {
1204+
for (FilterType item : items) {
1205+
EC2Filter oneFilter = new EC2Filter();
1206+
String filterName = item.getName();
1207+
oneFilter.setName( filterName );
1208+
1209+
ValueSetType vft = item.getValueSet();
1210+
ValueType[] valueItems = vft.getItem();
1211+
for (ValueType valueItem : valueItems) {
1212+
oneFilter.addValueEncoded( valueItem.getValue());
1213+
}
1214+
tfs.addFilter( oneFilter );
1215+
}
1216+
}
1217+
return tfs;
1218+
}
1219+
10991220
// toMethods
11001221
public static DescribeVolumesResponse toDescribeVolumesResponse( EC2DescribeVolumesResponse engineResponse )
11011222
{
@@ -1931,7 +2052,48 @@ public static RevokeSecurityGroupIngressResponse toRevokeSecurityGroupIngressRes
19312052
response.setRevokeSecurityGroupIngressResponse( param1 );
19322053
return response;
19332054
}
1934-
2055+
2056+
public static CreateTagsResponse toCreateTagsResponse( boolean success ) {
2057+
CreateTagsResponse response = new CreateTagsResponse();
2058+
CreateTagsResponseType param1 = new CreateTagsResponseType();
2059+
2060+
param1.set_return(success);
2061+
param1.setRequestId( UUID.randomUUID().toString());
2062+
response.setCreateTagsResponse(param1);
2063+
return response;
2064+
}
2065+
2066+
public static DeleteTagsResponse toDeleteTagsResponse( boolean success ) {
2067+
DeleteTagsResponse response = new DeleteTagsResponse();
2068+
DeleteTagsResponseType param1 = new DeleteTagsResponseType();
2069+
2070+
param1.set_return(success);
2071+
param1.setRequestId( UUID.randomUUID().toString());
2072+
response.setDeleteTagsResponse(param1);
2073+
return response;
2074+
}
2075+
2076+
public static DescribeTagsResponse toDescribeTagsResponse( EC2DescribeTagsResponse engineResponse) {
2077+
DescribeTagsResponse response = new DescribeTagsResponse();
2078+
DescribeTagsResponseType param1 = new DescribeTagsResponseType();
2079+
2080+
EC2ResourceTag[] tags = engineResponse.getTagsSet();
2081+
TagSetType param2 = new TagSetType();
2082+
for (EC2ResourceTag tag : tags) {
2083+
TagSetItemType param3 = new TagSetItemType();
2084+
param3.setResourceId(tag.getResourceId());
2085+
param3.setResourceType(tag.getResourceType());
2086+
param3.setKey(tag.getKey());
2087+
if (tag.getValue() != null)
2088+
param3.setValue(tag.getValue());
2089+
param2.addItem(param3);
2090+
}
2091+
param1.setTagSet(param2);
2092+
param1.setRequestId( UUID.randomUUID().toString());
2093+
response.setDescribeTagsResponse(param1);
2094+
return response;
2095+
}
2096+
19352097
public DescribeKeyPairsResponse describeKeyPairs(DescribeKeyPairs describeKeyPairs) {
19362098

19372099
EC2DescribeKeyPairs ec2Request = new EC2DescribeKeyPairs();
@@ -2116,10 +2278,6 @@ public CreateSpotDatafeedSubscriptionResponse createSpotDatafeedSubscription(Cre
21162278
public CreateSubnetResponse createSubnet(CreateSubnet createSubnet) {
21172279
throw new EC2ServiceException(ClientError.Unsupported, "This operation is not available");
21182280
}
2119-
2120-
public CreateTagsResponse createTags(CreateTags createTags) {
2121-
throw new EC2ServiceException(ClientError.Unsupported, "This operation is not available");
2122-
}
21232281

21242282
public CreateVpcResponse createVpc(CreateVpc createVpc) {
21252283
throw new EC2ServiceException(ClientError.Unsupported, "This operation is not available");
@@ -2156,10 +2314,6 @@ public DeleteSpotDatafeedSubscriptionResponse deleteSpotDatafeedSubscription(Del
21562314
public DeleteSubnetResponse deleteSubnet(DeleteSubnet deleteSubnet) {
21572315
throw new EC2ServiceException(ClientError.Unsupported, "This operation is not available");
21582316
}
2159-
2160-
public DeleteTagsResponse deleteTags(DeleteTags deleteTags) {
2161-
throw new EC2ServiceException(ClientError.Unsupported, "This operation is not available");
2162-
}
21632317

21642318
public DeleteVpcResponse deleteVpc(DeleteVpc deleteVpc) {
21652319
throw new EC2ServiceException(ClientError.Unsupported, "This operation is not available");
@@ -2229,10 +2383,6 @@ public DescribeSubnetsResponse describeSubnets(DescribeSubnets describeSubnets)
22292383
throw new EC2ServiceException(ClientError.Unsupported, "This operation is not available");
22302384
}
22312385

2232-
public DescribeTagsResponse describeTags(DescribeTags describeTags) {
2233-
throw new EC2ServiceException(ClientError.Unsupported, "This operation is not available");
2234-
}
2235-
22362386
public DescribeVpcsResponse describeVpcs(DescribeVpcs describeVpcs) {
22372387
throw new EC2ServiceException(ClientError.Unsupported, "This operation is not available");
22382388
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package com.cloud.bridge.service.core.ec2;
19+
20+
public class EC2DescribeTags {
21+
22+
private EC2TagsFilterSet tfs = null;
23+
24+
public EC2DescribeTags() {
25+
}
26+
27+
public EC2TagsFilterSet getFilterSet() {
28+
return tfs;
29+
}
30+
31+
public void setFilterSet( EC2TagsFilterSet param ) {
32+
tfs = param;
33+
}
34+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package com.cloud.bridge.service.core.ec2;
19+
20+
import java.util.ArrayList;
21+
import java.util.List;
22+
23+
public class EC2DescribeTagsResponse {
24+
25+
private List<EC2ResourceTag> tagsSet = new ArrayList<EC2ResourceTag>();
26+
27+
public EC2DescribeTagsResponse() {
28+
}
29+
30+
public void addTags( EC2ResourceTag param ) {
31+
tagsSet.add( param );
32+
}
33+
34+
public EC2ResourceTag[] getTagsSet() {
35+
return tagsSet.toArray(new EC2ResourceTag[0]);
36+
}
37+
}

0 commit comments

Comments
 (0)