Promoted Packet to the main repo
Project: http://git-wip-us.apache.org/repos/asf/jclouds-labs/repo Commit: http://git-wip-us.apache.org/repos/asf/jclouds-labs/commit/cb70f325 Tree: http://git-wip-us.apache.org/repos/asf/jclouds-labs/tree/cb70f325 Diff: http://git-wip-us.apache.org/repos/asf/jclouds-labs/diff/cb70f325 Branch: refs/heads/master Commit: cb70f3254c119ce69c3d0d46ef9b4af596ddc92f Parents: 85b3038 Author: Ignasi Barrera <[email protected]> Authored: Mon May 22 10:32:55 2017 +0200 Committer: Ignasi Barrera <[email protected]> Committed: Mon May 22 10:32:55 2017 +0200 ---------------------------------------------------------------------- packet/README.md | 10 - packet/pom.xml | 145 --- .../main/java/org/jclouds/packet/PacketApi.java | 86 -- .../org/jclouds/packet/PacketApiMetadata.java | 92 -- .../jclouds/packet/PacketProviderMetadata.java | 78 -- .../compute/PacketComputeServiceAdapter.java | 184 ---- .../PacketComputeServiceContextModule.java | 151 --- .../compute/functions/DeviceStateToStatus.java | 50 - .../compute/functions/DeviceToNodeMetadata.java | 110 --- .../compute/functions/FacilityToLocation.java | 55 -- .../functions/OperatingSystemToImage.java | 54 -- .../compute/functions/PlanToHardware.java | 86 -- .../compute/options/PacketTemplateOptions.java | 158 ---- .../strategy/CreateSshKeysThenCreateNodes.java | 215 ----- .../config/PacketComputeParserModule.java | 30 - .../packet/config/PacketHttpApiModule.java | 55 -- .../org/jclouds/packet/domain/ActionType.java | 39 - .../org/jclouds/packet/domain/BillingCycle.java | 56 -- .../java/org/jclouds/packet/domain/Device.java | 184 ---- .../org/jclouds/packet/domain/Distribution.java | 68 -- .../org/jclouds/packet/domain/Facility.java | 45 - .../java/org/jclouds/packet/domain/Href.java | 34 - .../org/jclouds/packet/domain/IpAddress.java | 59 -- .../jclouds/packet/domain/OperatingSystem.java | 45 - .../java/org/jclouds/packet/domain/Plan.java | 50 - .../java/org/jclouds/packet/domain/Pricing.java | 34 - .../java/org/jclouds/packet/domain/Project.java | 66 -- .../packet/domain/ProvisioningEvent.java | 52 -- .../java/org/jclouds/packet/domain/Specs.java | 98 -- .../java/org/jclouds/packet/domain/SshKey.java | 55 -- .../domain/internal/PaginatedCollection.java | 84 -- .../packet/domain/options/ListOptions.java | 60 -- .../org/jclouds/packet/features/DeviceApi.java | 163 ---- .../jclouds/packet/features/FacilityApi.java | 94 -- .../packet/features/OperatingSystemApi.java | 94 -- .../org/jclouds/packet/features/PlanApi.java | 94 -- .../org/jclouds/packet/features/ProjectApi.java | 94 -- .../org/jclouds/packet/features/SshKeyApi.java | 122 --- .../packet/filters/AddApiVersionToRequest.java | 57 -- .../packet/filters/AddXAuthTokenToRequest.java | 47 - .../packet/functions/BaseToPagedIterable.java | 59 -- .../packet/functions/HrefToListOptions.java | 63 -- .../packet/handlers/PacketErrorHandler.java | 64 -- .../packet/PacketProviderMetadataTest.java | 29 - .../PacketComputeProviderMetadataTest.java | 30 - .../compute/PacketComputeServiceLiveTest.java | 83 -- .../compute/PacketTemplateBuilderLiveTest.java | 55 -- .../compute/internal/BasePacketApiLiveTest.java | 81 -- .../compute/internal/BasePacketApiMockTest.java | 146 --- .../packet/features/DeviceApiLiveTest.java | 137 --- .../packet/features/DeviceApiMockTest.java | 174 ---- .../packet/features/FacilityApiLiveTest.java | 62 -- .../packet/features/FacilityApiMockTest.java | 78 -- .../features/OperatingSystemApiLiveTest.java | 62 -- .../features/OperatingSystemApiMockTest.java | 78 -- .../packet/features/PlanApiLiveTest.java | 62 -- .../packet/features/PlanApiMockTest.java | 78 -- .../packet/features/ProjectApiLiveTest.java | 62 -- .../packet/features/ProjectApiMockTest.java | 78 -- .../packet/features/SshKeyApiLiveTest.java | 83 -- .../packet/features/SshKeyApiMockTest.java | 134 --- .../packet/functions/HrefToListOptionsTest.java | 57 -- .../src/test/resources/device-create-req.json | 11 - .../src/test/resources/device-create-res.json | 211 ----- packet/src/test/resources/device.json | 278 ------ packet/src/test/resources/devices-first.json | 910 ------------------- packet/src/test/resources/devices-last.json | 376 -------- packet/src/test/resources/devices.json | 282 ------ packet/src/test/resources/facilities-first.json | 39 - packet/src/test/resources/facilities-last.json | 27 - packet/src/test/resources/facilities.json | 30 - packet/src/test/resources/logback-test.xml | 42 - .../test/resources/operatingSystems-first.json | 96 -- .../test/resources/operatingSystems-last.json | 106 --- packet/src/test/resources/operatingSystems.json | 166 ---- packet/src/test/resources/plans-first.json | 222 ----- packet/src/test/resources/plans-last.json | 98 -- packet/src/test/resources/plans.json | 284 ------ packet/src/test/resources/power-off.json | 3 - packet/src/test/resources/power-on.json | 3 - packet/src/test/resources/projects-first.json | 315 ------- packet/src/test/resources/projects-last.json | 197 ---- packet/src/test/resources/projects.json | 77 -- packet/src/test/resources/reboot.json | 3 - .../src/test/resources/ssh-key-create-req.json | 4 - .../src/test/resources/ssh-key-create-res.json | 12 - packet/src/test/resources/ssh-key.json | 12 - packet/src/test/resources/sshKeys-first.json | 80 -- packet/src/test/resources/sshKeys-last.json | 56 -- packet/src/test/resources/sshKeys.json | 16 - packet/src/test/resources/user.json | 19 - pom.xml | 1 - 92 files changed, 9084 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/cb70f325/packet/README.md ---------------------------------------------------------------------- diff --git a/packet/README.md b/packet/README.md deleted file mode 100644 index 49e7553..0000000 --- a/packet/README.md +++ /dev/null @@ -1,10 +0,0 @@ -# Apache jclouds Packet (packet.net) provider - - -## Pre-requisites - -If you are using Oracle JDK, you may get this exception -``` -Caused by: javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure -``` -when calling Packet endpoints. To solve this, you want to install JCE following the [official documentation](http://www.oracle.com/technetwork/java/javase/downloads/jce8-download-2133166.html). http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/cb70f325/packet/pom.xml ---------------------------------------------------------------------- diff --git a/packet/pom.xml b/packet/pom.xml deleted file mode 100644 index 8864894..0000000 --- a/packet/pom.xml +++ /dev/null @@ -1,145 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> -<!-- - - 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. - ---> -<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> - - <modelVersion>4.0.0</modelVersion> - <parent> - <groupId>org.apache.jclouds.labs</groupId> - <artifactId>jclouds-labs</artifactId> - <version>2.1.0-SNAPSHOT</version> - </parent> - - <!-- TODO: when out of labs, switch to org.jclouds.provider --> - <artifactId>packet</artifactId> - <name>jclouds Packet compute API</name> - <description>jclouds components to access an implementation of Packet's Compute Service</description> - <packaging>bundle</packaging> - - <properties> - <test.packet.endpoint>https://api.packet.net/</test.packet.endpoint> - <test.packet.identity>projectId</test.packet.identity> - <test.packet.credential>api key</test.packet.credential> - <jclouds.osgi.export>org.jclouds.packet*;version="${project.version}"</jclouds.osgi.export> - <jclouds.osgi.import>org.jclouds*;version="${project.version}",*</jclouds.osgi.import> - </properties> - - <dependencies> - <dependency> - <groupId>org.apache.jclouds</groupId> - <artifactId>jclouds-compute</artifactId> - <version>${project.parent.version}</version> - </dependency> - <dependency> - <groupId>com.google.auto.service</groupId> - <artifactId>auto-service</artifactId> - <scope>provided</scope> - </dependency> - <dependency> - <groupId>com.google.auto.value</groupId> - <artifactId>auto-value</artifactId> - <scope>provided</scope> - </dependency> - - <dependency> - <groupId>org.apache.jclouds</groupId> - <artifactId>jclouds-core</artifactId> - <version>${project.parent.version}</version> - <type>test-jar</type> - <scope>test</scope> - </dependency> - <dependency> - <groupId>org.apache.jclouds</groupId> - <artifactId>jclouds-compute</artifactId> - <version>${project.parent.version}</version> - <type>test-jar</type> - <scope>test</scope> - </dependency> - <dependency> - <groupId>org.apache.jclouds.driver</groupId> - <artifactId>jclouds-slf4j</artifactId> - <version>${project.parent.version}</version> - <type>test-jar</type> - <scope>test</scope> - </dependency> - <dependency> - <groupId>org.apache.jclouds.driver</groupId> - <artifactId>jclouds-slf4j</artifactId> - <version>${project.parent.version}</version> - <scope>test</scope> - </dependency> - <dependency> - <groupId>ch.qos.logback</groupId> - <artifactId>logback-classic</artifactId> - <scope>test</scope> - </dependency> - <dependency> - <groupId>org.apache.jclouds.driver</groupId> - <artifactId>jclouds-sshj</artifactId> - <version>${project.parent.version}</version> - <scope>test</scope> - </dependency> - <dependency> - <groupId>com.squareup.okhttp</groupId> - <artifactId>mockwebserver</artifactId> - <scope>test</scope> - <exclusions> - <!-- Already provided by jclouds-sshj --> - <exclusion> - <groupId>org.bouncycastle</groupId> - <artifactId>bcprov-jdk15on</artifactId> - </exclusion> - </exclusions> - </dependency> - </dependencies> - - <profiles> - <profile> - <id>live</id> - - <build> - <defaultGoal>clean verify</defaultGoal> - - <plugins> - <plugin> - <groupId>org.apache.maven.plugins</groupId> - <artifactId>maven-surefire-plugin</artifactId> - <executions> - <execution> - <id>integration</id> - <phase>integration-test</phase> - <goals> - <goal>test</goal> - </goals> - <configuration> - <systemPropertyVariables> - <test.packet.endpoint>${test.packet.endpoint}</test.packet.endpoint> - <test.packet.identity>${test.packet.identity}</test.packet.identity> - <test.packet.credential>${test.packet.credential}</test.packet.credential> - </systemPropertyVariables> - </configuration> - </execution> - </executions> - </plugin> - </plugins> - </build> - </profile> - </profiles> - -</project> http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/cb70f325/packet/src/main/java/org/jclouds/packet/PacketApi.java ---------------------------------------------------------------------- diff --git a/packet/src/main/java/org/jclouds/packet/PacketApi.java b/packet/src/main/java/org/jclouds/packet/PacketApi.java deleted file mode 100644 index e5f84e3..0000000 --- a/packet/src/main/java/org/jclouds/packet/PacketApi.java +++ /dev/null @@ -1,86 +0,0 @@ -/* - * 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.packet; - -import java.io.Closeable; - -import javax.ws.rs.PathParam; - -import org.jclouds.packet.features.DeviceApi; -import org.jclouds.packet.features.FacilityApi; -import org.jclouds.packet.features.OperatingSystemApi; -import org.jclouds.packet.features.PlanApi; -import org.jclouds.packet.features.ProjectApi; -import org.jclouds.packet.features.SshKeyApi; -import org.jclouds.rest.annotations.Delegate; - -/** - * The Packet API is a REST API for managing your services and deployments. - * <p> - * - * @see <a href="https://www.packet.net/help/api/" >doc</a> - */ -public interface PacketApi extends Closeable { - - /** - * The Packet API includes operations for managing project. - * - * @see <a href="https://www.packet.net/help/api/#page:projects,header:projects-projects">docs</a> - */ - @Delegate - ProjectApi projectApi(); - - /** - * This Packet API provides all of the devices - * - * @see <a href="https://www.packet.net/help/api/#page:devices">docs</a> - */ - @Delegate - DeviceApi deviceApi(@PathParam("projectId") String projectId); - - /** - * This Packet API provides all of the facilities - * - * @see <a href="https://www.packet.net/help/api/#page:devices,header:devices-operating-systems">docs</a> - */ - @Delegate - FacilityApi facilityApi(); - - /** - * This Packet API provides all of the plans - * - * @see <a href="https://www.packet.net/help/api/#page:devices,header:devices-plans">docs</a> - */ - @Delegate - PlanApi planApi(); - - /** - * This Packet API provides all of the operating systems - * - * @see <a href="https://www.packet.net/help/api/#page:devices,header:devices-operating-systems">docs</a> - */ - @Delegate - OperatingSystemApi operatingSystemApi(); - - /** - * This Packet API provides all of the operating systems - * - * @see <a href="https://www.packet.net/help/api/#page:ssh-keys">docs</a> - */ - @Delegate - SshKeyApi sshKeyApi(); -} http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/cb70f325/packet/src/main/java/org/jclouds/packet/PacketApiMetadata.java ---------------------------------------------------------------------- diff --git a/packet/src/main/java/org/jclouds/packet/PacketApiMetadata.java b/packet/src/main/java/org/jclouds/packet/PacketApiMetadata.java deleted file mode 100644 index 3d27de8..0000000 --- a/packet/src/main/java/org/jclouds/packet/PacketApiMetadata.java +++ /dev/null @@ -1,92 +0,0 @@ -/* - * 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.packet; - -import java.net.URI; -import java.util.Properties; - -import org.jclouds.apis.ApiMetadata; -import org.jclouds.compute.ComputeServiceContext; -import org.jclouds.packet.compute.config.PacketComputeServiceContextModule; -import org.jclouds.packet.config.PacketComputeParserModule; -import org.jclouds.packet.config.PacketHttpApiModule; -import org.jclouds.rest.internal.BaseHttpApiMetadata; - -import com.google.common.collect.ImmutableSet; -import com.google.inject.Module; - -import static org.jclouds.compute.config.ComputeServiceProperties.TEMPLATE; -import static org.jclouds.compute.config.ComputeServiceProperties.TIMEOUT_NODE_RUNNING; -import static org.jclouds.compute.config.ComputeServiceProperties.TIMEOUT_NODE_SUSPENDED; -import static org.jclouds.reflect.Reflection2.typeToken; - -/** - * Implementation of {@link ApiMetadata} for Packet API - */ -public class PacketApiMetadata extends BaseHttpApiMetadata<PacketApi> { - - @Override - public Builder toBuilder() { - return new Builder().fromApiMetadata(this); - } - - public PacketApiMetadata() { - this(new Builder()); - } - - protected PacketApiMetadata(Builder builder) { - super(builder); - } - - public static Properties defaultProperties() { - Properties properties = BaseHttpApiMetadata.defaultProperties(); - properties.put(TEMPLATE, "osFamily=UBUNTU,os64Bit=true,osVersionMatches=16.*"); - properties.put(TIMEOUT_NODE_RUNNING, 900000); // 15 mins - properties.put(TIMEOUT_NODE_SUSPENDED, 900000); // 15 mins - return properties; - } - - public static class Builder extends BaseHttpApiMetadata.Builder<PacketApi, Builder> { - - protected Builder() { - id("packet") - .name("Packet API") - .identityName("Packet Project Id") - .credentialName("Must be Packet Token") - .documentation(URI.create("https://www.packet.net/help/api/#")) - .defaultEndpoint("https://api.packet.net") - .defaultProperties(PacketApiMetadata.defaultProperties()) - .version("1") - .view(typeToken(ComputeServiceContext.class)) - .defaultModules(ImmutableSet.<Class<? extends Module>>builder() - .add(PacketHttpApiModule.class) - .add(PacketComputeParserModule.class) - .add(PacketComputeServiceContextModule.class) - .build()); - } - - @Override - public PacketApiMetadata build() { - return new PacketApiMetadata(this); - } - - @Override - protected Builder self() { - return this; - } - } -} http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/cb70f325/packet/src/main/java/org/jclouds/packet/PacketProviderMetadata.java ---------------------------------------------------------------------- diff --git a/packet/src/main/java/org/jclouds/packet/PacketProviderMetadata.java b/packet/src/main/java/org/jclouds/packet/PacketProviderMetadata.java deleted file mode 100644 index 69e0918..0000000 --- a/packet/src/main/java/org/jclouds/packet/PacketProviderMetadata.java +++ /dev/null @@ -1,78 +0,0 @@ -/* - * 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.packet; - -import java.net.URI; -import java.util.Properties; - -import org.jclouds.providers.ProviderMetadata; -import org.jclouds.providers.internal.BaseProviderMetadata; - -import com.google.auto.service.AutoService; - -@AutoService(ProviderMetadata.class) -public class PacketProviderMetadata extends BaseProviderMetadata { - - public static Builder builder() { - return new Builder(); - } - - @Override - public Builder toBuilder() { - return builder().fromProviderMetadata(this); - } - - public PacketProviderMetadata() { - super(builder()); - } - - public PacketProviderMetadata(Builder builder) { - super(builder); - } - - public static Properties defaultProperties() { - final Properties properties = PacketApiMetadata.defaultProperties(); - return properties; - } - - public static class Builder extends BaseProviderMetadata.Builder { - - protected Builder() { - id("packet") - .name("Packet Compute Services") - .apiMetadata(new PacketApiMetadata()) - .homepage(URI.create("https://www.packet.net/")) - .console(URI.create("https://app.packet.net/portal")) - .endpoint("https://api.packet.net") - .iso3166Codes("US-CA", "US-NJ", "NL", "JP") - .defaultProperties(PacketProviderMetadata.defaultProperties()); - } - - @Override - public PacketProviderMetadata build() { - return new PacketProviderMetadata(this); - } - - @Override - public Builder fromProviderMetadata(ProviderMetadata in) { - super.fromProviderMetadata(in); - return this; - } - } -} - - http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/cb70f325/packet/src/main/java/org/jclouds/packet/compute/PacketComputeServiceAdapter.java ---------------------------------------------------------------------- diff --git a/packet/src/main/java/org/jclouds/packet/compute/PacketComputeServiceAdapter.java b/packet/src/main/java/org/jclouds/packet/compute/PacketComputeServiceAdapter.java deleted file mode 100644 index 3672b14..0000000 --- a/packet/src/main/java/org/jclouds/packet/compute/PacketComputeServiceAdapter.java +++ /dev/null @@ -1,184 +0,0 @@ -/* - * 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.packet.compute; - -import java.util.Map; -import java.util.Set; - -import javax.annotation.Resource; -import javax.inject.Inject; -import javax.inject.Named; -import javax.inject.Singleton; - -import org.jclouds.compute.ComputeServiceAdapter; -import org.jclouds.compute.domain.Template; -import org.jclouds.compute.reference.ComputeServiceConstants; -import org.jclouds.domain.Credentials; -import org.jclouds.location.Provider; -import org.jclouds.logging.Logger; -import org.jclouds.packet.PacketApi; -import org.jclouds.packet.compute.options.PacketTemplateOptions; -import org.jclouds.packet.domain.BillingCycle; -import org.jclouds.packet.domain.Device; -import org.jclouds.packet.domain.Facility; -import org.jclouds.packet.domain.OperatingSystem; -import org.jclouds.packet.domain.Plan; - -import com.google.common.base.Optional; -import com.google.common.base.Predicate; -import com.google.common.base.Supplier; -import com.google.common.collect.Iterables; - -import static com.google.common.collect.Iterables.contains; -import static com.google.common.collect.Iterables.filter; - -/** - * defines the connection between the {@link org.jclouds.packet.PacketApi} implementation and - * the jclouds {@link org.jclouds.compute.ComputeService} - */ -@Singleton -public class PacketComputeServiceAdapter implements ComputeServiceAdapter<Device, Plan, OperatingSystem, Facility> { - - @Resource - @Named(ComputeServiceConstants.COMPUTE_LOGGER) - protected Logger logger = Logger.NULL; - - private final PacketApi api; - private final String projectId; - - @Inject - PacketComputeServiceAdapter(PacketApi api, @Provider final Supplier<Credentials> creds) { - this.api = api; - this.projectId = creds.get().identity; - } - - @Override - public NodeAndInitialCredentials<Device> createNodeWithGroupEncodedIntoName(String group, String name, Template template) { - - PacketTemplateOptions templateOptions = template.getOptions().as(PacketTemplateOptions.class); - Map<String, String> features = templateOptions.getFeatures(); - BillingCycle billingCycle = BillingCycle.fromValue(templateOptions.getBillingCycle()); - boolean locked = templateOptions.isLocked(); - String userdata = templateOptions.getUserData(); - Set<String> tags = templateOptions.getTags(); - - String plan = template.getHardware().getId(); - String facility = template.getLocation().getId(); - String operatingSystem = template.getImage().getId(); - - Device device = api.deviceApi(projectId).create( - Device.CreateDevice.builder() - .hostname(name) - .plan(plan) - .billingCycle(billingCycle.value()) - .facility(facility) - .features(features) - .operatingSystem(operatingSystem) - .locked(locked) - .userdata(userdata) - .tags(tags) - .build() - ); - - // Any new servers you deploy to projects you are a collaborator on will have your project and personal SSH keys, if defined. - // If no SSH keys are defined in your account, jclouds will generate one usiing CreateSshKeysThenCreateNodes - // so that it will add it to the device with the default mechanism. - - // Safe to pass null credentials here, as jclouds will default populate - // the node with the default credentials from the image, or the ones in - // the options, if provided. - return new NodeAndInitialCredentials<Device>(device, device.id(), null); - } - - @Override - public Iterable<Plan> listHardwareProfiles() { - return Iterables.filter(api.planApi().list().concat(), new Predicate<Plan>() { - @Override - public boolean apply(Plan input) { - return input.line().equals("baremetal"); - } - }); - } - - @Override - public Iterable<OperatingSystem> listImages() { - return api.operatingSystemApi().list().concat(); - } - - @Override - public OperatingSystem getImage(final String id) { - Optional<OperatingSystem> firstInterestingOperatingSystem = api - .operatingSystemApi().list() - .concat() - .firstMatch(new Predicate<OperatingSystem>() { - @Override - public boolean apply(OperatingSystem input) { - return input.slug().equals(id); - } - }); - if (!firstInterestingOperatingSystem.isPresent()) { - throw new IllegalStateException("Cannot find image with the required slug " + id); - } - return firstInterestingOperatingSystem.get(); - } - - @Override - public Iterable<Facility> listLocations() { - return api.facilityApi().list().concat(); - } - - @Override - public Device getNode(String id) { - return api.deviceApi(projectId).get(id); - } - - @Override - public void destroyNode(String id) { - api.deviceApi(projectId).delete(id); - } - - @Override - public void rebootNode(String id) { - api.deviceApi(projectId).reboot(id); - } - - @Override - public void resumeNode(String id) { - api.deviceApi(projectId).powerOn(id); - } - - @Override - public void suspendNode(String id) { - api.deviceApi(projectId).powerOff(id); - } - - @Override - public Iterable<Device> listNodes() { - return api.deviceApi(projectId).list().concat(); - } - - @Override - public Iterable<Device> listNodesByIds(final Iterable<String> ids) { - return filter(listNodes(), new Predicate<Device>() { - @Override - public boolean apply(Device device) { - return contains(ids, String.valueOf(device.id())); - } - }); - } - -} http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/cb70f325/packet/src/main/java/org/jclouds/packet/compute/config/PacketComputeServiceContextModule.java ---------------------------------------------------------------------- diff --git a/packet/src/main/java/org/jclouds/packet/compute/config/PacketComputeServiceContextModule.java b/packet/src/main/java/org/jclouds/packet/compute/config/PacketComputeServiceContextModule.java deleted file mode 100644 index 0a64f43..0000000 --- a/packet/src/main/java/org/jclouds/packet/compute/config/PacketComputeServiceContextModule.java +++ /dev/null @@ -1,151 +0,0 @@ -/* - * 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.packet.compute.config; - -import org.jclouds.compute.ComputeServiceAdapter; -import org.jclouds.compute.config.ComputeServiceAdapterContextModule; -import org.jclouds.compute.domain.Hardware; -import org.jclouds.compute.domain.Image; -import org.jclouds.compute.domain.NodeMetadata; -import org.jclouds.compute.options.TemplateOptions; -import org.jclouds.compute.reference.ComputeServiceConstants; -import org.jclouds.compute.strategy.CreateNodesInGroupThenAddToSet; -import org.jclouds.domain.Credentials; -import org.jclouds.domain.Location; -import org.jclouds.location.Provider; -import org.jclouds.packet.PacketApi; -import org.jclouds.packet.compute.PacketComputeServiceAdapter; -import org.jclouds.packet.compute.functions.DeviceStateToStatus; -import org.jclouds.packet.compute.functions.DeviceToNodeMetadata; -import org.jclouds.packet.compute.functions.FacilityToLocation; -import org.jclouds.packet.compute.functions.OperatingSystemToImage; -import org.jclouds.packet.compute.functions.PlanToHardware; -import org.jclouds.packet.compute.options.PacketTemplateOptions; -import org.jclouds.packet.compute.strategy.CreateSshKeysThenCreateNodes; -import org.jclouds.packet.domain.Device; -import org.jclouds.packet.domain.Facility; -import org.jclouds.packet.domain.OperatingSystem; -import org.jclouds.packet.domain.Plan; - -import com.google.common.annotations.VisibleForTesting; -import com.google.common.base.Function; -import com.google.common.base.Predicate; -import com.google.common.base.Supplier; -import com.google.inject.Provides; -import com.google.inject.TypeLiteral; -import com.google.inject.name.Named; - -import static com.google.common.base.Preconditions.checkNotNull; -import static org.jclouds.compute.config.ComputeServiceProperties.TIMEOUT_NODE_RUNNING; -import static org.jclouds.compute.config.ComputeServiceProperties.TIMEOUT_NODE_SUSPENDED; -import static org.jclouds.compute.config.ComputeServiceProperties.TIMEOUT_NODE_TERMINATED; -import static org.jclouds.util.Predicates2.retry; - -public class PacketComputeServiceContextModule extends - ComputeServiceAdapterContextModule<Device, Plan, OperatingSystem, Facility> { - - @SuppressWarnings("unchecked") - @Override - protected void configure() { - super.configure(); - - bind(new TypeLiteral<ComputeServiceAdapter<Device, Plan, OperatingSystem, Facility>>() { - }).to(PacketComputeServiceAdapter.class); - - bind(new TypeLiteral<Function<Device, NodeMetadata>>() { - }).to(DeviceToNodeMetadata.class); - bind(new TypeLiteral<Function<Plan, Hardware>>() { - }).to(PlanToHardware.class); - bind(new TypeLiteral<Function<OperatingSystem, Image>>() { - }).to(OperatingSystemToImage.class); - bind(new TypeLiteral<Function<Facility, Location>>() { - }).to(FacilityToLocation.class); - bind(new TypeLiteral<Function<Device.State, NodeMetadata.Status>>() { - }).to(DeviceStateToStatus.class); - install(new LocationsFromComputeServiceAdapterModule<Device, Plan, OperatingSystem, Facility>() { - }); - bind(TemplateOptions.class).to(PacketTemplateOptions.class); - bind(CreateNodesInGroupThenAddToSet.class).to(CreateSshKeysThenCreateNodes.class); - } - - @Provides - @Named(TIMEOUT_NODE_RUNNING) - protected Predicate<String> provideDeviceRunningPredicate(final PacketApi api, - @Provider final Supplier<Credentials> creds, - ComputeServiceConstants.Timeouts timeouts, - ComputeServiceConstants.PollPeriod pollPeriod) { - return retry(new DeviceInStatusPredicate(api, creds.get().identity, Device.State.ACTIVE), timeouts.nodeRunning, - pollPeriod.pollInitialPeriod, pollPeriod.pollMaxPeriod); - } - - @Provides - @Named(TIMEOUT_NODE_SUSPENDED) - protected Predicate<String> provideDeviceSuspendedPredicate(final PacketApi api, @Provider final Supplier<Credentials> creds, ComputeServiceConstants.Timeouts timeouts, - ComputeServiceConstants.PollPeriod pollPeriod) { - return retry(new DeviceInStatusPredicate(api, creds.get().identity, Device.State.INACTIVE), timeouts.nodeSuspended, - pollPeriod.pollInitialPeriod, pollPeriod.pollMaxPeriod); - } - - @Provides - @Named(TIMEOUT_NODE_TERMINATED) - protected Predicate<String> provideDeviceTerminatedPredicate(final PacketApi api, @Provider final Supplier<Credentials> creds, ComputeServiceConstants.Timeouts timeouts, - ComputeServiceConstants.PollPeriod pollPeriod) { - return retry(new DeviceTerminatedPredicate(api, creds.get().identity), timeouts.nodeTerminated, pollPeriod.pollInitialPeriod, - pollPeriod.pollMaxPeriod); - } - - @VisibleForTesting - static class DeviceInStatusPredicate implements Predicate<String> { - - private final PacketApi api; - private final String projectId; - private final Device.State state; - - public DeviceInStatusPredicate(PacketApi api, String projectId, Device.State state) { - this.api = checkNotNull(api, "api must not be null"); - this.projectId = checkNotNull(projectId, "projectId must not be null"); - this.state = checkNotNull(state, "state must not be null"); - } - - @Override - public boolean apply(String input) { - checkNotNull(input, "device id"); - Device device = api.deviceApi(projectId).get(input); - return device != null && state == device.state(); - } - } - - @VisibleForTesting - static class DeviceTerminatedPredicate implements Predicate<String> { - - private final PacketApi api; - private final String projectId; - - public DeviceTerminatedPredicate(PacketApi api, String projectId) { - this.api = checkNotNull(api, "api must not be null"); - this.projectId = checkNotNull(projectId, "projectId must not be null"); - } - - @Override - public boolean apply(String input) { - checkNotNull(input, "device id"); - Device device = api.deviceApi(projectId).get(input); - return device == null; - } - } - -} http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/cb70f325/packet/src/main/java/org/jclouds/packet/compute/functions/DeviceStateToStatus.java ---------------------------------------------------------------------- diff --git a/packet/src/main/java/org/jclouds/packet/compute/functions/DeviceStateToStatus.java b/packet/src/main/java/org/jclouds/packet/compute/functions/DeviceStateToStatus.java deleted file mode 100644 index 7606bf5..0000000 --- a/packet/src/main/java/org/jclouds/packet/compute/functions/DeviceStateToStatus.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * 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.packet.compute.functions; - -import javax.inject.Singleton; - -import org.jclouds.compute.domain.NodeMetadata.Status; -import org.jclouds.packet.domain.Device; - -import com.google.common.base.Function; -import com.google.common.base.Functions; -import com.google.common.collect.ImmutableMap; - -/** - * Transforms an {@link Device.State} to the jclouds portable model. - */ -@Singleton -public class DeviceStateToStatus implements Function<Device.State, Status> { - - private static final Function<Device.State, Status> toPortableStatus = Functions.forMap( - ImmutableMap.<Device.State, Status> builder() - .put(Device.State.PROVISIONING, Status.PENDING) - .put(Device.State.POWERING_ON, Status.PENDING) - .put(Device.State.POWERING_OFF, Status.PENDING) - .put(Device.State.REBOOTING, Status.PENDING) - .put(Device.State.QUEUED, Status.PENDING) - .put(Device.State.INACTIVE, Status.SUSPENDED) - .put(Device.State.ACTIVE, Status.RUNNING) - .build(), - Status.UNRECOGNIZED); - - @Override - public Status apply(final Device.State input) { - return toPortableStatus.apply(input); - } -} http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/cb70f325/packet/src/main/java/org/jclouds/packet/compute/functions/DeviceToNodeMetadata.java ---------------------------------------------------------------------- diff --git a/packet/src/main/java/org/jclouds/packet/compute/functions/DeviceToNodeMetadata.java b/packet/src/main/java/org/jclouds/packet/compute/functions/DeviceToNodeMetadata.java deleted file mode 100644 index ec222fc..0000000 --- a/packet/src/main/java/org/jclouds/packet/compute/functions/DeviceToNodeMetadata.java +++ /dev/null @@ -1,110 +0,0 @@ -/* - * 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.packet.compute.functions; - -import java.util.List; - -import javax.annotation.Resource; -import javax.inject.Inject; -import javax.inject.Named; -import javax.inject.Singleton; - -import org.jclouds.compute.domain.NodeMetadata; -import org.jclouds.compute.domain.NodeMetadataBuilder; -import org.jclouds.compute.functions.GroupNamingConvention; -import org.jclouds.compute.reference.ComputeServiceConstants; -import org.jclouds.logging.Logger; -import org.jclouds.packet.domain.Device; -import org.jclouds.packet.domain.IpAddress; - -import com.google.common.base.Function; -import com.google.common.base.Predicate; -import com.google.common.base.Predicates; - -import static com.google.common.collect.FluentIterable.from; - -/** - * Transforms an {@link Device} to the jclouds portable model. - */ -@Singleton -public class DeviceToNodeMetadata implements Function<Device, NodeMetadata> { - - @Resource - @Named(ComputeServiceConstants.COMPUTE_LOGGER) - protected Logger logger = Logger.NULL; - - private final PlanToHardware planToHardware; - private final OperatingSystemToImage operatingSystemToImage; - private final FacilityToLocation facilityToLocation; - private final Function<Device.State, NodeMetadata.Status> toPortableStatus; - private final GroupNamingConvention groupNamingConvention; - - @Inject - DeviceToNodeMetadata(PlanToHardware planToHardware, OperatingSystemToImage operatingSystemToImage, FacilityToLocation facilityToLocation, - Function<Device.State, NodeMetadata.Status> toPortableStatus, - GroupNamingConvention.Factory groupNamingConvention) { - this.planToHardware = planToHardware; - this.operatingSystemToImage = operatingSystemToImage; - this.facilityToLocation = facilityToLocation; - this.toPortableStatus = toPortableStatus; - this.groupNamingConvention = groupNamingConvention.createWithoutPrefix(); - } - - @Override - public NodeMetadata apply(Device input) { - return new NodeMetadataBuilder() - .ids(input.id()) - .name(input.hostname()) - .hostname(String.format("%s", input.hostname())) - .group(groupNamingConvention.extractGroup(input.hostname())) - .location(facilityToLocation.apply(input.facility())) - .hardware(planToHardware.apply(input.plan())) - .imageId(input.operatingSystem().slug()) - .operatingSystem(operatingSystemToImage.apply(input.operatingSystem()).getOperatingSystem()) - .status(toPortableStatus.apply(input.state())) - .publicAddresses(getPublicIpAddresses(input.ipAddresses())) - .privateAddresses(getPrivateIpAddresses(input.ipAddresses())) - .tags(input.tags()) - .build(); - } - - private Iterable<String> getPublicIpAddresses(List<IpAddress> input) { - return filterAndTransformIpAddresses(input, new IsPublicIpAddress()); - } - - private Iterable<String> getPrivateIpAddresses(List<IpAddress> input) { - return filterAndTransformIpAddresses(input, Predicates.not(new IsPublicIpAddress())); - } - - private Iterable<String> filterAndTransformIpAddresses(List<IpAddress> input, Predicate<IpAddress> filter) { - return from(input).filter(filter).transform(new IpAddressToIp()); - } - - private static class IpAddressToIp implements Function<IpAddress, String> { - @Override - public String apply(final IpAddress input) { - return input.address(); - } - } - - private static class IsPublicIpAddress implements Predicate<IpAddress> { - @Override - public boolean apply(IpAddress input) { - return input.publicAddress(); - } - } -} http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/cb70f325/packet/src/main/java/org/jclouds/packet/compute/functions/FacilityToLocation.java ---------------------------------------------------------------------- diff --git a/packet/src/main/java/org/jclouds/packet/compute/functions/FacilityToLocation.java b/packet/src/main/java/org/jclouds/packet/compute/functions/FacilityToLocation.java deleted file mode 100644 index c8df1bc..0000000 --- a/packet/src/main/java/org/jclouds/packet/compute/functions/FacilityToLocation.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * 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.packet.compute.functions; - -import javax.inject.Inject; -import javax.inject.Singleton; - -import org.jclouds.domain.Location; -import org.jclouds.domain.LocationBuilder; -import org.jclouds.domain.LocationScope; -import org.jclouds.location.suppliers.all.JustProvider; -import org.jclouds.packet.domain.Facility; - -import com.google.common.base.Function; - -import static com.google.common.collect.Iterables.getOnlyElement; - -/** - * Transforms an {@link Facility} to the jclouds portable model. - */ -@Singleton -public class FacilityToLocation implements Function<Facility, Location> { - - private final JustProvider justProvider; - - // allow us to lazy discover the provider of a resource - @Inject - FacilityToLocation(JustProvider justProvider) { - this.justProvider = justProvider; - } - - @Override - public Location apply(final Facility facility) { - final LocationBuilder builder = new LocationBuilder(); - builder.id(facility.code()); - builder.description(facility.name()); - builder.parent(getOnlyElement(justProvider.get())); - builder.scope(LocationScope.REGION); - return builder.build(); - } -} http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/cb70f325/packet/src/main/java/org/jclouds/packet/compute/functions/OperatingSystemToImage.java ---------------------------------------------------------------------- diff --git a/packet/src/main/java/org/jclouds/packet/compute/functions/OperatingSystemToImage.java b/packet/src/main/java/org/jclouds/packet/compute/functions/OperatingSystemToImage.java deleted file mode 100644 index d28b579..0000000 --- a/packet/src/main/java/org/jclouds/packet/compute/functions/OperatingSystemToImage.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * 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.packet.compute.functions; - -import javax.inject.Singleton; - -import org.jclouds.compute.domain.Image; -import org.jclouds.compute.domain.ImageBuilder; -import org.jclouds.packet.domain.Distribution; -import org.jclouds.packet.domain.OperatingSystem; - -import com.google.common.base.Function; - -import static org.jclouds.compute.domain.OperatingSystem.builder; - -/** - * Transforms an {@link OperatingSystem} to the jclouds portable model. - */ -@Singleton -public class OperatingSystemToImage implements Function<OperatingSystem, Image> { - - @Override - public Image apply(final OperatingSystem input) { - ImageBuilder builder = new ImageBuilder(); - builder.ids(input.slug()); - builder.name(input.name()); - builder.description(input.name()); - builder.status(Image.Status.AVAILABLE); - - builder.operatingSystem(builder() - .name(input.name()) - .family(Distribution.fromValue(input.distribution()).osFamily()) - .description(input.name()) - .version(input.version()) - .is64Bit(true) - .build()); - - return builder.build(); - } -} http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/cb70f325/packet/src/main/java/org/jclouds/packet/compute/functions/PlanToHardware.java ---------------------------------------------------------------------- diff --git a/packet/src/main/java/org/jclouds/packet/compute/functions/PlanToHardware.java b/packet/src/main/java/org/jclouds/packet/compute/functions/PlanToHardware.java deleted file mode 100644 index e6ac2ef..0000000 --- a/packet/src/main/java/org/jclouds/packet/compute/functions/PlanToHardware.java +++ /dev/null @@ -1,86 +0,0 @@ -/* - * 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.packet.compute.functions; - -import javax.inject.Singleton; - -import org.jclouds.compute.domain.Hardware; -import org.jclouds.compute.domain.HardwareBuilder; -import org.jclouds.compute.domain.Processor; -import org.jclouds.compute.domain.Volume; -import org.jclouds.compute.domain.internal.VolumeImpl; -import org.jclouds.packet.domain.Plan; -import org.jclouds.packet.domain.Specs; - -import com.google.common.base.Function; -import com.google.common.collect.Iterables; -import com.google.common.collect.Lists; - -/** - * Transforms an {@link Plan} to the jclouds portable model. - */ -@Singleton -public class PlanToHardware implements Function<Plan, Hardware> { - - @Override - public Hardware apply(Plan plan) { - HardwareBuilder builder = new HardwareBuilder() - .ids(plan.slug()) - .name(plan.name()) - .hypervisor("none") - .processors(getProcessors(plan)) - .ram(getMemory(plan)) - .volumes(getVolumes(plan)); - return builder.build(); - } - - private Integer getMemory(Plan plan) { - if (plan.specs() == null || plan.specs().drives() == null) return 0; - String total = plan.specs().memory().total(); - if (total.endsWith("GB")) { - return Integer.valueOf(total.substring(0, total.length() - 2)) * 1024; - } else { - throw new IllegalArgumentException("Cannot parse memory: " + plan.specs().memory()); - } - } - - private Iterable<Volume> getVolumes(Plan plan) { - if (plan.specs() == null || plan.specs().drives() == null) return Lists.newArrayList(); - - return Iterables.transform(plan.specs().drives(), new Function<Specs.Drive, Volume>() { - @Override - public Volume apply(Specs.Drive drive) { - return new VolumeImpl( - drive.type(), - Volume.Type.LOCAL, - Float.parseFloat(drive.size().substring(0, drive.size().length() - 2)), null, true, false); - } - }); - } - - - private Iterable<Processor> getProcessors(Plan plan) { - if (plan.specs() == null || plan.specs().cpus() == null) return Lists.newArrayList(); - return Iterables.transform(plan.specs().cpus(), new Function<Specs.CPU, Processor>() { - @Override - public Processor apply(Specs.CPU input) { - // No cpu speed from Packet API, so assume more cores == faster - return new Processor(input.count(), input.count()); - } - }); - } -} http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/cb70f325/packet/src/main/java/org/jclouds/packet/compute/options/PacketTemplateOptions.java ---------------------------------------------------------------------- diff --git a/packet/src/main/java/org/jclouds/packet/compute/options/PacketTemplateOptions.java b/packet/src/main/java/org/jclouds/packet/compute/options/PacketTemplateOptions.java deleted file mode 100644 index 30cef45..0000000 --- a/packet/src/main/java/org/jclouds/packet/compute/options/PacketTemplateOptions.java +++ /dev/null @@ -1,158 +0,0 @@ -/* - * 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.packet.compute.options; - -import java.util.Map; - -import org.jclouds.compute.options.TemplateOptions; - -import com.google.common.base.Objects; -import com.google.common.base.Objects.ToStringHelper; -import com.google.common.collect.ImmutableMap; - -import static com.google.common.base.Objects.equal; -import static com.google.common.base.Preconditions.checkNotNull; - -/** - * Custom options for the Packet API. - */ -public class PacketTemplateOptions extends TemplateOptions implements Cloneable { - - private Map<String, String> features = ImmutableMap.of(); - private boolean locked = false; - private String billingCycle = "hourly"; - private String userData = ""; - - public PacketTemplateOptions features(Map<String, String> features) { - this.features = ImmutableMap.copyOf(checkNotNull(features, "features cannot be null")); - return this; - } - - public PacketTemplateOptions locked(boolean locked) { - this.locked = locked; - return this; - } - - public PacketTemplateOptions billingCycle(String billingCycle) { - this.billingCycle = billingCycle; - return this; - } - - public PacketTemplateOptions userData(String userData) { - this.userData = userData; - return this; - } - - public Map<String, String> getFeatures() { - return features; - } - public boolean isLocked() { - return locked; - } - public String getBillingCycle() { - return billingCycle; - } - public String getUserData() { - return userData; - } - - @Override - public PacketTemplateOptions clone() { - PacketTemplateOptions options = new PacketTemplateOptions(); - copyTo(options); - return options; - } - - @Override - public void copyTo(TemplateOptions to) { - super.copyTo(to); - if (to instanceof PacketTemplateOptions) { - PacketTemplateOptions eTo = PacketTemplateOptions.class.cast(to); - eTo.features(features); - eTo.locked(locked); - eTo.billingCycle(billingCycle); - eTo.userData(userData); - } - } - - @Override - public int hashCode() { - return Objects.hashCode(super.hashCode(), features, locked, billingCycle, userData); - } - - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (!super.equals(obj)) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - PacketTemplateOptions other = (PacketTemplateOptions) obj; - return super.equals(other) && equal(this.locked, other.locked) && equal(this.billingCycle, other.billingCycle) && equal(this.userData, other.userData) && equal(this.features, other.features); - } - - @Override - public ToStringHelper string() { - ToStringHelper toString = super.string().omitNullValues(); - if (!features.isEmpty()) { - toString.add("features", features); - } toString.add("locked", locked); - toString.add("billingCycle", billingCycle); - toString.add("userData", userData); - return toString; - } - - public static class Builder { - - /** - * @see PacketTemplateOptions#features - */ - public static PacketTemplateOptions features(Map<String, String> features) { - PacketTemplateOptions options = new PacketTemplateOptions(); - return options.features(features); - } - - /** - * @see PacketTemplateOptions#locked - */ - public static PacketTemplateOptions locked(boolean locked) { - PacketTemplateOptions options = new PacketTemplateOptions(); - return options.locked(locked); - } - - /** - * @see PacketTemplateOptions#billingCycle - */ - public static PacketTemplateOptions billingCycle(String billingCycle) { - PacketTemplateOptions options = new PacketTemplateOptions(); - return options.billingCycle(billingCycle); - } - - /** - * @see PacketTemplateOptions#userData - */ - public static PacketTemplateOptions userData(String userData) { - PacketTemplateOptions options = new PacketTemplateOptions(); - return options.userData(userData); - } - - } -} http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/cb70f325/packet/src/main/java/org/jclouds/packet/compute/strategy/CreateSshKeysThenCreateNodes.java ---------------------------------------------------------------------- diff --git a/packet/src/main/java/org/jclouds/packet/compute/strategy/CreateSshKeysThenCreateNodes.java b/packet/src/main/java/org/jclouds/packet/compute/strategy/CreateSshKeysThenCreateNodes.java deleted file mode 100644 index 3a5a02a..0000000 --- a/packet/src/main/java/org/jclouds/packet/compute/strategy/CreateSshKeysThenCreateNodes.java +++ /dev/null @@ -1,215 +0,0 @@ -/* - * 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.packet.compute.strategy; - -import java.security.KeyFactory; -import java.security.NoSuchAlgorithmException; -import java.security.PublicKey; -import java.security.interfaces.RSAPublicKey; -import java.security.spec.InvalidKeySpecException; -import java.security.spec.RSAPublicKeySpec; -import java.util.List; -import java.util.Map; -import java.util.Set; - -import javax.annotation.Resource; -import javax.inject.Inject; -import javax.inject.Named; -import javax.inject.Singleton; - -import org.jclouds.Constants; -import org.jclouds.compute.config.CustomizationResponse; -import org.jclouds.compute.domain.NodeMetadata; -import org.jclouds.compute.domain.Template; -import org.jclouds.compute.functions.GroupNamingConvention; -import org.jclouds.compute.reference.ComputeServiceConstants; -import org.jclouds.compute.strategy.CreateNodeWithGroupEncodedIntoName; -import org.jclouds.compute.strategy.CustomizeNodeAndAddToGoodMapOrPutExceptionIntoBadMap; -import org.jclouds.compute.strategy.ListNodesStrategy; -import org.jclouds.compute.strategy.impl.CreateNodesWithGroupEncodedIntoNameThenAddToSet; -import org.jclouds.logging.Logger; -import org.jclouds.packet.PacketApi; -import org.jclouds.packet.compute.options.PacketTemplateOptions; -import org.jclouds.packet.domain.SshKey; -import org.jclouds.ssh.SshKeyPairGenerator; -import org.jclouds.ssh.SshKeys; - -import com.google.common.base.Splitter; -import com.google.common.base.Strings; -import com.google.common.collect.Multimap; -import com.google.common.collect.Sets; -import com.google.common.util.concurrent.FutureCallback; -import com.google.common.util.concurrent.Futures; -import com.google.common.util.concurrent.ListenableFuture; -import com.google.common.util.concurrent.ListeningExecutorService; - -import static com.google.common.base.Preconditions.checkArgument; -import static com.google.common.base.Throwables.propagate; -import static com.google.common.collect.Iterables.get; -import static com.google.common.collect.Iterables.size; - -@Singleton -public class CreateSshKeysThenCreateNodes extends CreateNodesWithGroupEncodedIntoNameThenAddToSet { - - @Resource - @Named(ComputeServiceConstants.COMPUTE_LOGGER) - protected Logger logger = Logger.NULL; - - private final PacketApi api; - private final SshKeyPairGenerator keyGenerator; - - @Inject - protected CreateSshKeysThenCreateNodes( - CreateNodeWithGroupEncodedIntoName addNodeWithGroupStrategy, - ListNodesStrategy listNodesStrategy, - GroupNamingConvention.Factory namingConvention, - @Named(Constants.PROPERTY_USER_THREADS) ListeningExecutorService userExecutor, - CustomizeNodeAndAddToGoodMapOrPutExceptionIntoBadMap.Factory customizeNodeAndAddToGoodMapOrPutExceptionIntoBadMapFactory, - PacketApi api, SshKeyPairGenerator keyGenerator) { - super(addNodeWithGroupStrategy, listNodesStrategy, namingConvention, userExecutor, - customizeNodeAndAddToGoodMapOrPutExceptionIntoBadMapFactory); - this.api = api; - this.keyGenerator = keyGenerator; - } - - @Override - public Map<?, ListenableFuture<Void>> execute(String group, int count, Template template, - Set<NodeMetadata> goodNodes, Map<NodeMetadata, Exception> badNodes, - Multimap<NodeMetadata, CustomizationResponse> customizationResponses) { - - PacketTemplateOptions options = template.getOptions().as(PacketTemplateOptions.class); - Set<String> generatedSshKeyIds = Sets.newHashSet(); - - // If no key has been configured, generate a key pair - if (Strings.isNullOrEmpty(options.getPublicKey())) { - generateKeyPairAndAddKeyToSet(options, generatedSshKeyIds, group); - } - - // If there is a script to run in the node, make sure a private key has - // been configured so jclouds will be able to access the node - if (options.getRunScript() != null && Strings.isNullOrEmpty(options.getLoginPrivateKey())) { - logger.warn(">> A runScript has been configured but no SSH key has been provided." - + " Authentication will delegate to the ssh-agent"); - } - - // If there is a key configured, then make sure there is a key pair for it - if (!Strings.isNullOrEmpty(options.getPublicKey())) { - createKeyPairForPublicKeyInOptionsAndAddToSet(options, generatedSshKeyIds); - } - - Map<?, ListenableFuture<Void>> responses = super.execute(group, count, template, goodNodes, badNodes, - customizationResponses); - - // Key pairs in Packet are only required to create the devices. They aren't used anymore so it is better - // to delete the auto-generated key pairs at this point where we know exactly which ones have been - // auto-generated by jclouds. - registerAutoGeneratedKeyPairCleanupCallbacks(responses, generatedSshKeyIds); - - return responses; - } - - private void createKeyPairForPublicKeyInOptionsAndAddToSet(PacketTemplateOptions options, - Set<String> generatedSshKeyIds) { - logger.debug(">> checking if the key pair already exists..."); - - PublicKey userKey; - Iterable<String> parts = Splitter.on(' ').split(options.getPublicKey()); - checkArgument(size(parts) >= 2, "bad format, should be: ssh-rsa AAAAB3..."); - String type = get(parts, 0); - - try { - if ("ssh-rsa".equals(type)) { - RSAPublicKeySpec spec = SshKeys.publicKeySpecFromOpenSSH(options.getPublicKey()); - userKey = KeyFactory.getInstance("RSA").generatePublic(spec); - } else { - throw new IllegalArgumentException("bad format, ssh-rsa is only supported"); - } - } catch (InvalidKeySpecException ex) { - throw propagate(ex); - } catch (NoSuchAlgorithmException ex) { - throw propagate(ex); - } - String label = computeFingerprint(userKey); - SshKey key = api.sshKeyApi().get(label); - - if (key == null) { - logger.debug(">> key pair not found. creating a new key pair %s ...", label); - SshKey newKey = api.sshKeyApi().create(label, options.getPublicKey()); - logger.debug(">> key pair created! %s", newKey); - } else { - logger.debug(">> key pair found! %s", key); - generatedSshKeyIds.add(key.id()); - } - } - - private void generateKeyPairAndAddKeyToSet(PacketTemplateOptions options, Set<String> generatedSshKeyIds, String prefix) { - logger.debug(">> creating default keypair for node..."); - - Map<String, String> defaultKeys = keyGenerator.get(); - - SshKey sshKey = api.sshKeyApi().create(prefix + System.getProperty("user.name"), defaultKeys.get("public")); - generatedSshKeyIds.add(sshKey.id()); - logger.debug(">> keypair created! %s", sshKey); - - // If a private key has not been explicitly set, configure the generated one - if (Strings.isNullOrEmpty(options.getLoginPrivateKey())) { - options.overrideLoginPrivateKey(defaultKeys.get("private")); - } - } - - private void registerAutoGeneratedKeyPairCleanupCallbacks(Map<?, ListenableFuture<Void>> responses, - final Set<String> generatedSshKeyIds) { - // The Futures.allAsList fails immediately if some of the futures fail. The Futures.successfulAsList, however, - // returns a list containing the results or 'null' for those futures that failed. We want to wait for all them - // (even if they fail), so better use the latter form. - ListenableFuture<List<Void>> aggregatedResponses = Futures.successfulAsList(responses.values()); - - // Key pairs must be cleaned up after all futures completed (even if some failed). - Futures.addCallback(aggregatedResponses, new FutureCallback<List<Void>>() { - @Override - public void onSuccess(List<Void> result) { - cleanupAutoGeneratedKeyPairs(generatedSshKeyIds); - } - - @Override - public void onFailure(Throwable t) { - cleanupAutoGeneratedKeyPairs(generatedSshKeyIds); - } - - private void cleanupAutoGeneratedKeyPairs(Set<String> generatedSshKeyIds) { - logger.debug(">> cleaning up auto-generated key pairs..."); - for (String sshKeyId : generatedSshKeyIds) { - try { - api.sshKeyApi().delete(sshKeyId); - } catch (Exception ex) { - logger.warn(">> could not delete key pair %s: %s", sshKeyId, ex.getMessage()); - } - } - } - }, userExecutor); - } - - private static String computeFingerprint(PublicKey key) { - if (key instanceof RSAPublicKey) { - RSAPublicKey rsaKey = (RSAPublicKey) key; - return SshKeys.fingerprint(rsaKey.getPublicExponent(), rsaKey.getModulus()); - } else { - throw new IllegalArgumentException("Only RSA keys are supported"); - } - } - -} http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/cb70f325/packet/src/main/java/org/jclouds/packet/config/PacketComputeParserModule.java ---------------------------------------------------------------------- diff --git a/packet/src/main/java/org/jclouds/packet/config/PacketComputeParserModule.java b/packet/src/main/java/org/jclouds/packet/config/PacketComputeParserModule.java deleted file mode 100644 index 8471fc4..0000000 --- a/packet/src/main/java/org/jclouds/packet/config/PacketComputeParserModule.java +++ /dev/null @@ -1,30 +0,0 @@ -/* - * 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.packet.config; - -import org.jclouds.json.config.GsonModule; - -import com.google.inject.AbstractModule; - -public class PacketComputeParserModule extends AbstractModule { - - @Override - protected void configure() { - bind(GsonModule.DateAdapter.class).to(GsonModule.Iso8601DateAdapter.class); - } - -} http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/cb70f325/packet/src/main/java/org/jclouds/packet/config/PacketHttpApiModule.java ---------------------------------------------------------------------- diff --git a/packet/src/main/java/org/jclouds/packet/config/PacketHttpApiModule.java b/packet/src/main/java/org/jclouds/packet/config/PacketHttpApiModule.java deleted file mode 100644 index 6edeebd..0000000 --- a/packet/src/main/java/org/jclouds/packet/config/PacketHttpApiModule.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * 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.packet.config; - -import org.jclouds.http.HttpErrorHandler; -import org.jclouds.http.annotation.ClientError; -import org.jclouds.http.annotation.Redirection; -import org.jclouds.http.annotation.ServerError; -import org.jclouds.location.suppliers.ImplicitLocationSupplier; -import org.jclouds.location.suppliers.implicit.FirstRegion; -import org.jclouds.packet.PacketApi; -import org.jclouds.packet.domain.Href; -import org.jclouds.packet.domain.options.ListOptions; -import org.jclouds.packet.functions.HrefToListOptions; -import org.jclouds.packet.handlers.PacketErrorHandler; -import org.jclouds.rest.ConfiguresHttpApi; -import org.jclouds.rest.config.HttpApiModule; - -import com.google.common.base.Function; -import com.google.inject.Scopes; -import com.google.inject.TypeLiteral; - -@ConfiguresHttpApi -public class PacketHttpApiModule extends HttpApiModule<PacketApi> { - - @Override - protected void configure() { - super.configure(); - bind(ImplicitLocationSupplier.class).to(FirstRegion.class).in(Scopes.SINGLETON); - bind(new TypeLiteral<Function<Href, ListOptions>>() { - }).to(HrefToListOptions.class); - } - - @Override - protected void bindErrorHandlers() { - bind(HttpErrorHandler.class).annotatedWith(Redirection.class).to(PacketErrorHandler.class); - bind(HttpErrorHandler.class).annotatedWith(ClientError.class).to(PacketErrorHandler.class); - bind(HttpErrorHandler.class).annotatedWith(ServerError.class).to(PacketErrorHandler.class); - } - -} http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/cb70f325/packet/src/main/java/org/jclouds/packet/domain/ActionType.java ---------------------------------------------------------------------- diff --git a/packet/src/main/java/org/jclouds/packet/domain/ActionType.java b/packet/src/main/java/org/jclouds/packet/domain/ActionType.java deleted file mode 100644 index 9177ec5..0000000 --- a/packet/src/main/java/org/jclouds/packet/domain/ActionType.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * 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.packet.domain; - -/** - * Performs an action for the given device. Possible actions include: - - power_on - power_off - reboot - rescue: reboot the device into rescue OS. - */ -public enum ActionType { - - POWER_ON ("power_on"), - POWER_OFF ("power_off"), - REBOOT ("reboot"), - RESCUE ("rescue"); - - private final String type; - - ActionType(String type) { - this.type = type; - } -} http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/cb70f325/packet/src/main/java/org/jclouds/packet/domain/BillingCycle.java ---------------------------------------------------------------------- diff --git a/packet/src/main/java/org/jclouds/packet/domain/BillingCycle.java b/packet/src/main/java/org/jclouds/packet/domain/BillingCycle.java deleted file mode 100644 index ef88946..0000000 --- a/packet/src/main/java/org/jclouds/packet/domain/BillingCycle.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * 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.packet.domain; - -import java.util.List; - -import com.google.common.base.Predicate; - -import static com.google.common.base.Preconditions.checkNotNull; -import static com.google.common.collect.Iterables.tryFind; -import static java.util.Arrays.asList; - -public enum BillingCycle { - HOURLY("hourly"), - MONTHLY("monthly"), - UNRECOGNIZED(""); - - private static final List<BillingCycle> values = asList(BillingCycle.values()); - - private final String value; - - private BillingCycle(String value) { - this.value = checkNotNull(value, "value cannot be null"); - } - - public String value() { - return this.value; - } - - public static BillingCycle fromValue(String value) { - return tryFind(values, hasValue(value)).or(UNRECOGNIZED); - } - - private static Predicate<BillingCycle> hasValue(final String value) { - return new Predicate<BillingCycle>() { - @Override - public boolean apply(BillingCycle input) { - return input.value.equalsIgnoreCase(value); - } - }; - } -} http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/cb70f325/packet/src/main/java/org/jclouds/packet/domain/Device.java ---------------------------------------------------------------------- diff --git a/packet/src/main/java/org/jclouds/packet/domain/Device.java b/packet/src/main/java/org/jclouds/packet/domain/Device.java deleted file mode 100644 index 18f5d95..0000000 --- a/packet/src/main/java/org/jclouds/packet/domain/Device.java +++ /dev/null @@ -1,184 +0,0 @@ -/* - * 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.packet.domain; - -import java.util.Date; -import java.util.List; -import java.util.Map; -import java.util.Set; - -import org.jclouds.javax.annotation.Nullable; -import org.jclouds.json.SerializedNames; - -import com.google.auto.value.AutoValue; -import com.google.common.base.Enums; -import com.google.common.base.Joiner; -import com.google.common.base.Optional; -import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableMap; -import com.google.common.collect.ImmutableSet; - -import static com.google.common.base.Preconditions.checkArgument; - -@AutoValue -public abstract class Device { - - public enum State { - PROVISIONING, QUEUED, ACTIVE, REBOOTING, POWERING_OFF, POWERING_ON, INACTIVE; - - public static State fromValue(String value) { - Optional<State> state = Enums.getIfPresent(State.class, value.toUpperCase()); - checkArgument(state.isPresent(), "Expected one of %s but was %s", Joiner.on(',').join(State.values()), value); - return state.get(); - } - } - - public abstract String id(); - public abstract String shortId(); - public abstract String hostname(); - @Nullable - public abstract String description(); - public abstract State state(); - public abstract List<String> tags(); - public abstract String billingCycle(); - public abstract String user(); - public abstract String iqn(); - public abstract Boolean locked(); - public abstract String bondingMode(); - public abstract Date createdAt(); - public abstract Date updatedAt(); - public abstract OperatingSystem operatingSystem(); - public abstract Facility facility(); - public abstract Href project(); - public abstract List<Href> sshKeys(); - public abstract Href projectLite(); - public abstract List<Object> volumes(); - public abstract List<IpAddress> ipAddresses(); - public abstract List<ProvisioningEvent> provisioningEvents(); - public abstract Plan plan(); - @Nullable public abstract String rootPassword(); - public abstract String userdata(); - public abstract String href(); - - @SerializedNames({"id", "short_id", "hostname", "description", "state", "tags", "billing_cycle", "user", "iqn", - "locked", "bonding_mode", "created_at", "updated_at", "operating_system", "facility", "project", "ssh_keys", - "project_lite", "volumes", "ip_addresses", "provisioning_events", "plan", "root_password", "userdata", "href"}) - public static Device create(String id, - String shortId, - String hostname, - String description, - State state, - List<String> tags, - String billingCycle, - String user, - String iqn, - Boolean locked, - String bondingMode, - Date createdAt, - Date updatedAt, - OperatingSystem operatingSystem, - Facility facility, - Href project, - List<Href> sshKeys, - Href projectLite, - List<Object> volumes, - List<IpAddress> ipAddresses, - List<ProvisioningEvent> provisioningEvents, - Plan plan, - String rootPassword, - String userdata, - String href - ) { - return new AutoValue_Device(id, shortId, hostname, description, state, - tags == null ? ImmutableList.<String> of() : ImmutableList.copyOf(tags), - billingCycle, user, iqn, locked, bondingMode, createdAt, updatedAt, operatingSystem, facility, project, - sshKeys == null ? ImmutableList.<Href> of() : ImmutableList.copyOf(sshKeys), - projectLite, - volumes == null ? ImmutableList.of() : ImmutableList.copyOf(volumes), - ipAddresses == null ? ImmutableList.<IpAddress>of() : ImmutableList.copyOf(ipAddresses), - provisioningEvents == null ? ImmutableList.<ProvisioningEvent> of() : ImmutableList.copyOf(provisioningEvents), - plan, - rootPassword, userdata, href - ); - } - - Device() { - } - - @AutoValue - public abstract static class CreateDevice { - - public abstract String hostname(); - public abstract String plan(); - public abstract String billingCycle(); - public abstract String facility(); - public abstract Map<String, String> features(); - public abstract String operatingSystem(); - public abstract Boolean locked(); - public abstract String userdata(); - public abstract Set<String> tags(); - - @SerializedNames({"hostname", "plan", "billing_cycle", "facility", "features", "operating_system", - "locked", "userdata", "tags" }) - private static CreateDevice create(final String hostname, final String plan, final String billingCycle, - final String facility, final Map<String, String> features, final String operatingSystem, - final Boolean locked, final String userdata, - final Set<String> tags) { - return builder() - .hostname(hostname) - .plan(plan) - .billingCycle(billingCycle) - .facility(facility) - .features(features) - .operatingSystem(operatingSystem) - .locked(locked) - .userdata(userdata) - .tags(tags) - .build(); - } - - public static Builder builder() { - return new AutoValue_Device_CreateDevice.Builder(); - } - - @AutoValue.Builder - public abstract static class Builder { - - public abstract Builder hostname(String hostname); - public abstract Builder plan(String plan); - public abstract Builder billingCycle(String billingCycle); - public abstract Builder facility(String facility); - public abstract Builder features(Map<String, String> features); - public abstract Builder operatingSystem(String operatingSystem); - public abstract Builder locked(Boolean locked); - public abstract Builder userdata(String userdata); - public abstract Builder tags(Set<String> tags); - - abstract Map<String, String> features(); - abstract Set<String> tags(); - - abstract CreateDevice autoBuild(); - - public CreateDevice build() { - return tags(tags() != null ? ImmutableSet.copyOf(tags()) : ImmutableSet.<String> of()) - .features(features() != null ? ImmutableMap.copyOf(features()) : ImmutableMap.<String, String> of()) - .autoBuild(); - } - } - } - -}
