You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jclouds.apache.org by na...@apache.org on 2017/05/22 08:25:31 UTC

[07/12] jclouds git commit: add compute functions

add compute functions

- fix Device and PaginatedCollection
- improve DeviceToNodeMetadata


Project: http://git-wip-us.apache.org/repos/asf/jclouds/repo
Commit: http://git-wip-us.apache.org/repos/asf/jclouds/commit/36964122
Tree: http://git-wip-us.apache.org/repos/asf/jclouds/tree/36964122
Diff: http://git-wip-us.apache.org/repos/asf/jclouds/diff/36964122

Branch: refs/heads/master
Commit: 3696412213a3626917af6e8855671c546d0dae0b
Parents: 49f1d07
Author: Andrea Turli <an...@gmail.com>
Authored: Tue Jan 24 22:33:48 2017 +0100
Committer: Andrea Turli <an...@gmail.com>
Committed: Wed Jan 25 16:31:38 2017 +0100

----------------------------------------------------------------------
 .../compute/functions/DeviceStateToStatus.java  |  45 ++++++++
 .../compute/functions/DeviceToNodeMetadata.java | 110 +++++++++++++++++++
 .../compute/functions/FacilityToLocation.java   |  55 ++++++++++
 .../functions/OperatingSystemToImage.java       |  54 +++++++++
 .../compute/functions/PlanToHardware.java       |  86 +++++++++++++++
 .../java/org/jclouds/packet/domain/Device.java  |   2 +-
 .../domain/internal/PaginatedCollection.java    |  10 +-
 7 files changed, 356 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/jclouds/blob/36964122/providers/packet/src/main/java/org/jclouds/packet/compute/functions/DeviceStateToStatus.java
----------------------------------------------------------------------
diff --git a/providers/packet/src/main/java/org/jclouds/packet/compute/functions/DeviceStateToStatus.java b/providers/packet/src/main/java/org/jclouds/packet/compute/functions/DeviceStateToStatus.java
new file mode 100644
index 0000000..acb1f00
--- /dev/null
+++ b/providers/packet/src/main/java/org/jclouds/packet/compute/functions/DeviceStateToStatus.java
@@ -0,0 +1,45 @@
+/*
+ * 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.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/blob/36964122/providers/packet/src/main/java/org/jclouds/packet/compute/functions/DeviceToNodeMetadata.java
----------------------------------------------------------------------
diff --git a/providers/packet/src/main/java/org/jclouds/packet/compute/functions/DeviceToNodeMetadata.java b/providers/packet/src/main/java/org/jclouds/packet/compute/functions/DeviceToNodeMetadata.java
new file mode 100644
index 0000000..74d9d95
--- /dev/null
+++ b/providers/packet/src/main/java/org/jclouds/packet/compute/functions/DeviceToNodeMetadata.java
@@ -0,0 +1,110 @@
+/*
+ * 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(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/blob/36964122/providers/packet/src/main/java/org/jclouds/packet/compute/functions/FacilityToLocation.java
----------------------------------------------------------------------
diff --git a/providers/packet/src/main/java/org/jclouds/packet/compute/functions/FacilityToLocation.java b/providers/packet/src/main/java/org/jclouds/packet/compute/functions/FacilityToLocation.java
new file mode 100644
index 0000000..c8df1bc
--- /dev/null
+++ b/providers/packet/src/main/java/org/jclouds/packet/compute/functions/FacilityToLocation.java
@@ -0,0 +1,55 @@
+/*
+ * 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/blob/36964122/providers/packet/src/main/java/org/jclouds/packet/compute/functions/OperatingSystemToImage.java
----------------------------------------------------------------------
diff --git a/providers/packet/src/main/java/org/jclouds/packet/compute/functions/OperatingSystemToImage.java b/providers/packet/src/main/java/org/jclouds/packet/compute/functions/OperatingSystemToImage.java
new file mode 100644
index 0000000..d28b579
--- /dev/null
+++ b/providers/packet/src/main/java/org/jclouds/packet/compute/functions/OperatingSystemToImage.java
@@ -0,0 +1,54 @@
+/*
+ * 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/blob/36964122/providers/packet/src/main/java/org/jclouds/packet/compute/functions/PlanToHardware.java
----------------------------------------------------------------------
diff --git a/providers/packet/src/main/java/org/jclouds/packet/compute/functions/PlanToHardware.java b/providers/packet/src/main/java/org/jclouds/packet/compute/functions/PlanToHardware.java
new file mode 100644
index 0000000..e6ac2ef
--- /dev/null
+++ b/providers/packet/src/main/java/org/jclouds/packet/compute/functions/PlanToHardware.java
@@ -0,0 +1,86 @@
+/*
+ * 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/blob/36964122/providers/packet/src/main/java/org/jclouds/packet/domain/Device.java
----------------------------------------------------------------------
diff --git a/providers/packet/src/main/java/org/jclouds/packet/domain/Device.java b/providers/packet/src/main/java/org/jclouds/packet/domain/Device.java
index 96e0b53..951d938 100644
--- a/providers/packet/src/main/java/org/jclouds/packet/domain/Device.java
+++ b/providers/packet/src/main/java/org/jclouds/packet/domain/Device.java
@@ -70,7 +70,7 @@ public abstract class Device {
     public abstract List<IpAddress> ipAddresses();
     public abstract List<ProvisioningEvent> provisioningEvents();
     public abstract Plan plan();
-    public abstract String rootPassword();
+    @Nullable public abstract String rootPassword();
     public abstract String userdata();
     public abstract String href();
 

http://git-wip-us.apache.org/repos/asf/jclouds/blob/36964122/providers/packet/src/main/java/org/jclouds/packet/domain/internal/PaginatedCollection.java
----------------------------------------------------------------------
diff --git a/providers/packet/src/main/java/org/jclouds/packet/domain/internal/PaginatedCollection.java b/providers/packet/src/main/java/org/jclouds/packet/domain/internal/PaginatedCollection.java
index 047151e..14621bc 100644
--- a/providers/packet/src/main/java/org/jclouds/packet/domain/internal/PaginatedCollection.java
+++ b/providers/packet/src/main/java/org/jclouds/packet/domain/internal/PaginatedCollection.java
@@ -16,8 +16,6 @@
  */
 package org.jclouds.packet.domain.internal;
 
-import static com.google.common.base.Preconditions.checkNotNull;
-
 import java.util.Iterator;
 import java.util.List;
 
@@ -30,6 +28,8 @@ import com.google.auto.value.AutoValue;
 import com.google.common.base.Optional;
 import com.google.common.collect.ImmutableList;
 
+import static com.google.common.base.Preconditions.checkNotNull;
+
 /**
  * Base class for all collections that return paginated results.
  */
@@ -57,7 +57,7 @@ public abstract class PaginatedCollection<T> extends IterableWithMarker<T> {
 
    protected PaginatedCollection(List<T> items, Meta meta) {
       this.items = ImmutableList.copyOf(checkNotNull(items, "items cannot be null"));
-      this.meta = checkNotNull(meta, "meta cannot be null");
+      this.meta = meta;
    }
 
    public List<T> items() {
@@ -66,7 +66,7 @@ public abstract class PaginatedCollection<T> extends IterableWithMarker<T> {
 
    public Meta meta() {
       return meta;
-   }
+   }  
 
    @Override
    public Iterator<T> iterator() {
@@ -75,7 +75,7 @@ public abstract class PaginatedCollection<T> extends IterableWithMarker<T> {
 
    @Override
    public Optional<Object> nextMarker() {
-      if (meta.next() == null) {
+      if (meta == null || meta.next() == null) {
          return Optional.absent();
       }
       return Optional.fromNullable((Object) meta.next());