Skip to content
Permalink
Browse files
JCLOUDS-215 - Adds Group configuration, Group launch, Scaling policy …
…functionality
  • Loading branch information
zack-shoylev committed Nov 1, 2013
1 parent e3c8147 commit 1c871e12e4bd3ea4bd5d208189f63e892eaf23aa
Showing 36 changed files with 1,918 additions and 87 deletions.
@@ -25,7 +25,6 @@
import java.net.URI;
import java.util.Properties;

import org.jclouds.apis.ApiMetadata;
import org.jclouds.openstack.keystone.v2_0.config.KeystoneAuthenticationModule.ZoneModule;
import org.jclouds.providers.ProviderMetadata;
import org.jclouds.providers.internal.BaseProviderMetadata;
@@ -24,7 +24,7 @@
*
* @author Zack Shoylev
*/
@Test(groups = "unit", testName = "AutoscaleUSGroupApiLiveTest")
@Test(groups = "unit", testName = "AutoscaleUSGroupApiLiveTest", singleThreaded = true)
public class AutoscaleUSGroupApiLiveTest extends GroupApiLiveTest {
public AutoscaleUSGroupApiLiveTest() {
provider = "rackspace-autoscale-us";
@@ -0,0 +1,32 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jclouds.rackspace.autoscale.us.v1.features;

import org.jclouds.rackspace.autoscale.v1.features.ScalingPolicyApiLiveTest;
import org.testng.annotations.Test;

/**
* Tests GroupApi Guice wiring and parsing
*
* @author Zack Shoylev
*/
@Test(groups = "unit", testName = "AutoscaleUSGroupApiLiveTest", singleThreaded = true)
public class AutoscaleUSScalingPolicyApiLiveTest extends ScalingPolicyApiLiveTest {
public AutoscaleUSScalingPolicyApiLiveTest() {
provider = "rackspace-autoscale-us";
}
}
@@ -19,11 +19,15 @@
import java.io.Closeable;
import java.util.Set;

import javax.ws.rs.Path;
import javax.ws.rs.PathParam;

import org.jclouds.javax.annotation.Nullable;
import org.jclouds.location.Zone;
import org.jclouds.location.functions.ZoneToEndpoint;
import org.jclouds.openstack.keystone.v2_0.domain.Tenant;
import org.jclouds.rackspace.autoscale.v1.features.GroupApi;
import org.jclouds.rackspace.autoscale.v1.features.PolicyApi;
import org.jclouds.rest.annotations.Delegate;
import org.jclouds.rest.annotations.EndpointParam;

@@ -53,6 +57,14 @@ public interface AutoscaleApi extends Closeable{
@Delegate
GroupApi getGroupApiForZone(@EndpointParam(parser = ZoneToEndpoint.class) @Nullable String zone);

/**
* Provides access to all policy features for scaling Groups.
*/
@Delegate
@Path("/groups/{groupId}")
PolicyApi getPolicyApiForGroupInZone(@PathParam("groupId") String groupId,
@EndpointParam(parser = ZoneToEndpoint.class) @Nullable String zone);

/**
* Provides the Tenant.
*/
@@ -16,18 +16,16 @@
*/
package org.jclouds.rackspace.autoscale.v1.binders;

import java.util.List;
import java.util.Map;

import org.jclouds.http.HttpRequest;
import org.jclouds.rackspace.autoscale.v1.domain.GroupConfiguration;
import org.jclouds.rackspace.autoscale.v1.domain.LaunchConfiguration;
import org.jclouds.rackspace.autoscale.v1.domain.ScalingPolicy;
import org.jclouds.rackspace.autoscale.v1.internal.ParseHelper;
import org.jclouds.rest.MapBinder;
import org.jclouds.rest.binders.BindToJsonPayload;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.inject.Inject;

/**
@@ -43,46 +41,12 @@ private BindCreateGroupToJson(BindToJsonPayload jsonBinder) {
this.jsonBinder = jsonBinder;
}

@SuppressWarnings("unchecked")
@Override
public <R extends HttpRequest> R bindToRequest(R request, Map<String, Object> postParams) {
List<ScalingPolicy> scalingPoliciesRequest = (List<ScalingPolicy>) postParams.get("scalingPolicies");
LaunchConfiguration launchConfigurationRequest = (LaunchConfiguration) postParams.get("launchConfiguration");

ImmutableMap<String, Object> launchConfigurationMap = ParseHelper.buildLaunchConfigurationRequestMap(postParams);
GroupConfiguration groupConfiguration = (GroupConfiguration) postParams.get("groupConfiguration");

Map<String, Object> launchConfigurationMap = Maps.newHashMap();
List<Map<String, Object>> scalingPoliciesList = Lists.newArrayList();
Map<String, Object> args = Maps.newHashMap();
launchConfigurationMap.put("args", args);
launchConfigurationMap.put("type", launchConfigurationRequest.getType().toString());

args.put("loadBalancers", launchConfigurationRequest.getLoadBalancers());
Map<String, Object> server = Maps.newHashMap();
args.put("server", server);
server.put("name", launchConfigurationRequest.getServerName());
server.put("imageRef", launchConfigurationRequest.getServerImageRef());
server.put("flavorRef", launchConfigurationRequest.getServerFlavorRef());
server.put("OS-DCF:diskConfig", launchConfigurationRequest.getServerDiskConfig());
server.put("metadata", launchConfigurationRequest.getServerMetadata());
List<Map<String, String>> networks = Lists.newArrayList();
server.put("networks", networks);
for(String networkId : launchConfigurationRequest.getNetworks()) {
Map<String, String> network = Maps.newHashMap();
network.put("uuid", networkId);
networks.add(network);
}
server.put("personality", launchConfigurationRequest.getPersonalities());

for(ScalingPolicy scalingPolicy : scalingPoliciesRequest) {
Map<String,Object> scalingPolicyMap = Maps.newHashMap();
scalingPoliciesList.add(scalingPolicyMap);
scalingPolicyMap.put("cooldown", scalingPolicy.getCooldown());
scalingPolicyMap.put("type", scalingPolicy.getType().toString());
scalingPolicyMap.put("name", scalingPolicy.getName());
// A couple of different scaling policies are supported, such as percent or number based, or targeting specific numbers of instances
scalingPolicyMap.put(scalingPolicy.getTargetType().toString(), scalingPolicy.getTarget());
}
ImmutableList<Map<String, Object>> scalingPoliciesList = ParseHelper.buildScalingPoliciesRequestList(postParams);

return jsonBinder.bindToRequest(request, ImmutableMap.of(
"launchConfiguration", launchConfigurationMap,
@@ -92,6 +56,6 @@ public <R extends HttpRequest> R bindToRequest(R request, Map<String, Object> po

@Override
public <R extends HttpRequest> R bindToRequest(R request, Object toBind) {
throw new IllegalStateException("CreateInstance is a POST operation");
throw new IllegalStateException("Create scaling group is a POST operation");
}
}
@@ -0,0 +1,50 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jclouds.rackspace.autoscale.v1.binders;

import java.util.Map;

import org.jclouds.http.HttpRequest;
import org.jclouds.rackspace.autoscale.v1.internal.ParseHelper;
import org.jclouds.rest.MapBinder;
import org.jclouds.rest.binders.BindToJsonPayload;

import com.google.inject.Inject;

/**
* Decouple building the json object from the domain objects structure by using the binder
* @author Zack Shoylev
*/
public class BindLaunchConfigurationToJson implements MapBinder {

private final BindToJsonPayload jsonBinder;

@Inject
private BindLaunchConfigurationToJson(BindToJsonPayload jsonBinder) {
this.jsonBinder = jsonBinder;
}

@Override
public <R extends HttpRequest> R bindToRequest(R request, Map<String, Object> postParams) {
return jsonBinder.bindToRequest(request, ParseHelper.buildLaunchConfigurationRequestMap(postParams));
}

@Override
public <R extends HttpRequest> R bindToRequest(R request, Object toBind) {
throw new IllegalStateException("Update launch configuration is a POST operation");
}
}
@@ -0,0 +1,50 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jclouds.rackspace.autoscale.v1.binders;

import java.util.Map;

import org.jclouds.http.HttpRequest;
import org.jclouds.rackspace.autoscale.v1.internal.ParseHelper;
import org.jclouds.rest.MapBinder;
import org.jclouds.rest.binders.BindToJsonPayload;

import com.google.inject.Inject;

/**
* Decouple building the json object from the domain objects structure by using the binder
* @author Zack Shoylev
*/
public class BindScalingPoliciesToJson implements MapBinder {

private final BindToJsonPayload jsonBinder;

@Inject
private BindScalingPoliciesToJson(BindToJsonPayload jsonBinder) {
this.jsonBinder = jsonBinder;
}

@Override
public <R extends HttpRequest> R bindToRequest(R request, Map<String, Object> postParams) {
return jsonBinder.bindToRequest(request, ParseHelper.buildScalingPoliciesRequestList(postParams));
}

@Override
public <R extends HttpRequest> R bindToRequest(R request, Object toBind) {
throw new IllegalStateException("Create Policies is a POST operation");
}
}
@@ -0,0 +1,52 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jclouds.rackspace.autoscale.v1.binders;

import java.util.Map;

import org.jclouds.http.HttpRequest;
import org.jclouds.rackspace.autoscale.v1.domain.ScalingPolicy;
import org.jclouds.rackspace.autoscale.v1.internal.ParseHelper;
import org.jclouds.rest.MapBinder;
import org.jclouds.rest.binders.BindToJsonPayload;

import com.google.inject.Inject;

/**
* Decouple building the json object from the domain objects structure by using the binder
* @author Zack Shoylev
*/
public class BindScalingPolicyToJson implements MapBinder {

private final BindToJsonPayload jsonBinder;

@Inject
private BindScalingPolicyToJson(BindToJsonPayload jsonBinder) {
this.jsonBinder = jsonBinder;
}

@Override
public <R extends HttpRequest> R bindToRequest(R request, Map<String, Object> postParams) {
ScalingPolicy scalingPolicy = (ScalingPolicy) postParams.get("scalingPolicy");
return jsonBinder.bindToRequest(request, ParseHelper.buildScalingPolicyMap(scalingPolicy));
}

@Override
public <R extends HttpRequest> R bindToRequest(R request, Object toBind) {
throw new IllegalStateException("Update policy is a POST operation");
}
}
@@ -0,0 +1,64 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jclouds.rackspace.autoscale.v1.binders;

import static com.google.common.base.Preconditions.checkNotNull;

import java.util.Map;

import javax.inject.Inject;

import org.jclouds.http.HttpRequest;
import org.jclouds.json.Json;
import org.jclouds.rackspace.autoscale.v1.domain.GroupConfiguration;
import org.jclouds.rest.MapBinder;

import com.google.common.collect.Maps;

/**
*
* @author Zack Shoylev
*/
public class BindToGroupConfigurationRequestPayload implements MapBinder {

protected final Json jsonBinder;

@Inject
public BindToGroupConfigurationRequestPayload(Json jsonBinder) {
this.jsonBinder = checkNotNull(jsonBinder, "jsonBinder");
}

@Override
public <R extends HttpRequest> R bindToRequest(R request, Map<String, Object> postParams) {
GroupConfiguration gc = (GroupConfiguration) postParams.get("groupConfiguration");
Map<String, Object> gcMap = Maps.newHashMap();
gcMap.put("name", gc.getName());
gcMap.put("cooldown", gc.getCooldown());
gcMap.put("minEntities", gc.getMinEntities());
gcMap.put("maxEntities", gc.getMaxEntities());
gcMap.put("metadata", gc.getMetadata());

request.setPayload(jsonBinder.toJson(gcMap));
request.getPayload().getContentMetadata().setContentType("application/json");
return request;
}

@Override
public <R extends HttpRequest> R bindToRequest(R request, Object toBind) {
throw new IllegalStateException("Illegal unwrap operation");
}
}

0 comments on commit 1c871e1

Please sign in to comment.