| 1 | /** |
| 2 | * |
| 3 | * Copyright (C) 2011 Cloud Conscious, LLC. <info@cloudconscious.com> |
| 4 | * |
| 5 | * ==================================================================== |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | * ==================================================================== |
| 18 | */ |
| 19 | package org.jclouds.ec2.domain; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | |
| 23 | import javax.annotation.Nullable; |
| 24 | |
| 25 | /** |
| 26 | * |
| 27 | * @see <a href= |
| 28 | * "http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/ApiReference-query-CreateKeyPair.html" |
| 29 | * /> |
| 30 | * @author Adrian Cole |
| 31 | */ |
| 32 | public class KeyPair implements Comparable<KeyPair> { |
| 33 | @Override |
| 34 | public String toString() { |
| 35 | return "[region=" + region + ", keyName=" + keyName + ", keyFingerprint=" + keyFingerprint + ", keyMaterial?=" |
| 36 | + (keyMaterial != null) + "]"; |
| 37 | } |
| 38 | |
| 39 | public static Builder builder() { |
| 40 | return new Builder(); |
| 41 | } |
| 42 | |
| 43 | public static class Builder { |
| 44 | private String region; |
| 45 | private String keyName; |
| 46 | private String keyFingerprint; |
| 47 | private String keyMaterial; |
| 48 | |
| 49 | public Builder region(String region) { |
| 50 | this.region = region; |
| 51 | return this; |
| 52 | } |
| 53 | |
| 54 | public Builder keyName(String keyName) { |
| 55 | this.keyName = keyName; |
| 56 | return this; |
| 57 | } |
| 58 | |
| 59 | public Builder keyFingerprint(String keyFingerprint) { |
| 60 | this.keyFingerprint = keyFingerprint; |
| 61 | return this; |
| 62 | } |
| 63 | |
| 64 | public Builder keyMaterial(String keyMaterial) { |
| 65 | this.keyMaterial = keyMaterial; |
| 66 | return this; |
| 67 | } |
| 68 | |
| 69 | public KeyPair build() { |
| 70 | return new KeyPair(region, keyName, keyFingerprint, keyMaterial); |
| 71 | } |
| 72 | |
| 73 | public static Builder fromKeyPair(KeyPair in) { |
| 74 | return new Builder().region(in.getRegion()).keyName(in.getKeyName()).keyFingerprint(in.getKeyFingerprint()) |
| 75 | .keyMaterial(in.getKeyMaterial()); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | private final String region; |
| 80 | private final String keyName; |
| 81 | private final String keyFingerprint; |
| 82 | @Nullable |
| 83 | private final String keyMaterial; |
| 84 | |
| 85 | public KeyPair(String region, String keyName, String keyFingerprint, @Nullable String keyMaterial) { |
| 86 | this.region = checkNotNull(region, "region"); |
| 87 | this.keyName = checkNotNull(keyName, "keyName"); |
| 88 | this.keyFingerprint = checkNotNull(keyFingerprint, "keyFingerprint"); |
| 89 | this.keyMaterial = keyMaterial;// nullable on list |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Key pairs (to connect to instances) are Region-specific. |
| 94 | */ |
| 95 | public String getRegion() { |
| 96 | return region; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * {@inheritDoc} |
| 101 | */ |
| 102 | public int compareTo(KeyPair o) { |
| 103 | return (this == o) ? 0 : getKeyName().compareTo(o.getKeyName()); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * A SHA-1 digest of the DER encoded private key. |
| 108 | */ |
| 109 | public String getKeyFingerprint() { |
| 110 | return keyFingerprint; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * An unencrypted PEM encoded RSA private key. |
| 115 | */ |
| 116 | public String getKeyMaterial() { |
| 117 | return keyMaterial; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * The key pair name provided in the original request. |
| 122 | */ |
| 123 | public String getKeyName() { |
| 124 | return keyName; |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public int hashCode() { |
| 129 | final int prime = 31; |
| 130 | int result = 1; |
| 131 | result = prime * result + ((keyFingerprint == null) ? 0 : keyFingerprint.hashCode()); |
| 132 | result = prime * result + ((keyMaterial == null) ? 0 : keyMaterial.hashCode()); |
| 133 | result = prime * result + ((keyName == null) ? 0 : keyName.hashCode()); |
| 134 | result = prime * result + ((region == null) ? 0 : region.hashCode()); |
| 135 | return result; |
| 136 | } |
| 137 | |
| 138 | @Override |
| 139 | public boolean equals(Object obj) { |
| 140 | if (this == obj) |
| 141 | return true; |
| 142 | if (obj == null) |
| 143 | return false; |
| 144 | if (getClass() != obj.getClass()) |
| 145 | return false; |
| 146 | KeyPair other = (KeyPair) obj; |
| 147 | if (keyFingerprint == null) { |
| 148 | if (other.keyFingerprint != null) |
| 149 | return false; |
| 150 | } else if (!keyFingerprint.equals(other.keyFingerprint)) |
| 151 | return false; |
| 152 | if (keyMaterial == null) { |
| 153 | if (other.keyMaterial != null) |
| 154 | return false; |
| 155 | } else if (!keyMaterial.equals(other.keyMaterial)) |
| 156 | return false; |
| 157 | if (keyName == null) { |
| 158 | if (other.keyName != null) |
| 159 | return false; |
| 160 | } else if (!keyName.equals(other.keyName)) |
| 161 | return false; |
| 162 | if (region == null) { |
| 163 | if (other.region != null) |
| 164 | return false; |
| 165 | } else if (!region.equals(other.region)) |
| 166 | return false; |
| 167 | return true; |
| 168 | } |
| 169 | |
| 170 | public Builder toBuilder() { |
| 171 | return Builder.fromKeyPair(this); |
| 172 | } |
| 173 | } |