|
5 | 5 |
|
6 | 6 | package com.oracle.oci.osb.adapters.adb;
|
7 | 7 |
|
| 8 | +import com.oracle.bmc.Region; |
| 9 | +import com.oracle.bmc.auth.AuthenticationDetailsProvider; |
| 10 | +import com.oracle.bmc.database.DatabaseClient; |
| 11 | +import com.oracle.bmc.database.model.*; |
| 12 | +import com.oracle.bmc.database.requests.*; |
| 13 | +import com.oracle.bmc.database.responses.*; |
| 14 | +import com.oracle.oci.osb.util.Constants; |
| 15 | +import com.oracle.oci.osb.util.RequestUtil; |
| 16 | +import com.oracle.oci.osb.util.Utils; |
| 17 | + |
8 | 18 | import java.io.IOException;
|
| 19 | +import java.util.ArrayList; |
9 | 20 | import java.util.List;
|
10 | 21 | import java.util.Map;
|
| 22 | +import java.util.logging.Level; |
| 23 | +import java.util.logging.Logger; |
| 24 | + |
| 25 | +import static com.oracle.oci.osb.util.Utils.debugLog; |
| 26 | +import static com.oracle.oci.osb.util.Utils.getLogger; |
| 27 | + |
| 28 | +public class AutonomousDatabaseOCIClient implements AutoCloseable{ |
| 29 | + |
| 30 | + private static final Logger LOGGER = getLogger(AutonomousDatabaseOCIClient.class); |
| 31 | + |
| 32 | + private DatabaseClient ociDBClient; |
| 33 | + |
| 34 | + private String compartmentId; |
| 35 | + |
| 36 | + AutonomousDatabaseOCIClient(AuthenticationDetailsProvider authProvider, String compartmentId) { |
| 37 | + this(authProvider, compartmentId, Region.fromRegionId(System.getProperty(Constants.REGION_ID))); |
| 38 | + } |
| 39 | + |
| 40 | + private AutonomousDatabaseOCIClient(AuthenticationDetailsProvider authProvider, String compartmentId, Region regionId) { |
| 41 | + this.compartmentId = compartmentId; |
| 42 | + ociDBClient = new DatabaseClient(authProvider); |
| 43 | + ociDBClient.setRegion(regionId); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Create an AD instance. The instance will be provisioned asynchronously |
| 48 | + * |
| 49 | + * @param displayName display name(mostly for console) |
| 50 | + * @param dbName name of the database. |
| 51 | + * @param type DBWorkload Type of the Database. OLTP or DW. |
| 52 | + * @param cpuCount number of cpu cores required. |
| 53 | + * @param StorageSize required storage size for DB in Terabytes. |
| 54 | + * @param tags freeform tags. |
| 55 | + * @param password password to be set for the DB admin user. |
| 56 | + * @return AutonomousDatabase |
| 57 | + */ |
| 58 | + public AutonomousDatabaseInstance create(String displayName, String dbName, CreateAutonomousDatabaseBase.DbWorkload type, |
| 59 | + int cpuCount, int StorageSize, Map<String, String> tags, |
| 60 | + Map<String, Map<String, Object>> definedTags, String password, |
| 61 | + boolean licenseIncluded) { |
| 62 | + CreateAutonomousDatabaseDetails request = CreateAutonomousDatabaseDetails.builder() |
| 63 | + .adminPassword(password) |
| 64 | + .compartmentId(compartmentId) |
| 65 | + .cpuCoreCount(cpuCount) |
| 66 | + .dbWorkload(type) |
| 67 | + .dataStorageSizeInTBs(StorageSize) |
| 68 | + .dbName(dbName) |
| 69 | + .displayName(displayName) |
| 70 | + .freeformTags(tags) |
| 71 | + .definedTags(definedTags) |
| 72 | + .licenseModel(licenseIncluded ? CreateAutonomousDatabaseDetails |
| 73 | + .LicenseModel.LicenseIncluded : CreateAutonomousDatabaseDetails.LicenseModel |
| 74 | + .BringYourOwnLicense) |
| 75 | + .build(); |
| 76 | + CreateAutonomousDatabaseResponse response = ociDBClient.createAutonomousDatabase |
| 77 | + (CreateAutonomousDatabaseRequest.builder().createAutonomousDatabaseDetails(request).build()); |
| 78 | + |
| 79 | + return buildADInstance(response.getAutonomousDatabase()); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Update an AD Instance. Update will be done asynchronously. If the param |
| 84 | + * values are empty or if they already match the existing value then update |
| 85 | + * is skipped for those values. If none of the params have any change then |
| 86 | + * this simply returns without doing any update. |
| 87 | + * |
| 88 | + * @param adOCID OCID of the AD instance to be updated. |
| 89 | + * @param displayName new display name. |
| 90 | + * @param cpuCount new number of CPU core. |
| 91 | + * @param StorageSize new DB storage size in Terabytes. |
| 92 | + * @param tags new freeform tags. |
| 93 | + * @return AutonomousDatabase |
| 94 | + */ |
| 95 | + public AutonomousDatabaseInstance update(String adOCID, String displayName, int cpuCount, int StorageSize, |
| 96 | + Map<String, String> tags, Map<String, Map<String, Object>> definedTags) { |
| 97 | + AutonomousDatabase ad = getADInstance(adOCID); |
| 98 | + UpdateAutonomousDatabaseDetails.Builder reqBuilder = UpdateAutonomousDatabaseDetails.builder(); |
| 99 | + |
| 100 | + //Check if update required |
| 101 | + boolean updateRequired = false; |
| 102 | + if (!Utils.isNullOrEmptyString(displayName) && !ad.getDisplayName().equals(displayName)) { |
| 103 | + reqBuilder = reqBuilder.displayName(displayName); |
| 104 | + debugLog(LOGGER, "DisplayName to be updated.from:%s;to:%s", Level.FINE, ad.getDisplayName(), displayName); |
| 105 | + updateRequired = true; |
| 106 | + } |
| 107 | + if (cpuCount > 0 && ad.getCpuCoreCount() != cpuCount) { |
| 108 | + reqBuilder = reqBuilder.cpuCoreCount(cpuCount); |
| 109 | + debugLog(LOGGER, "CpuCoreCount to be updated.from:%s;to:%s", Level.FINE, ad.getCpuCoreCount() |
| 110 | + .toString(), Integer.toString(cpuCount)); |
| 111 | + updateRequired = true; |
| 112 | + } |
| 113 | + if (StorageSize > 0 && ad.getDataStorageSizeInTBs() != StorageSize) { |
| 114 | + reqBuilder = reqBuilder.dataStorageSizeInTBs(StorageSize); |
| 115 | + debugLog(LOGGER, "StorageSize to be updated.from:%s;to:%s", Level.FINE, ad.getDataStorageSizeInTBs() |
| 116 | + .toString(), Integer.toString(StorageSize)); |
| 117 | + updateRequired = true; |
| 118 | + } |
| 119 | + if (tags != null && tags.entrySet().size() > 0 && !tags.equals(ad.getFreeformTags())) { |
| 120 | + reqBuilder = reqBuilder.freeformTags(tags); |
| 121 | + debugLog(LOGGER, "tags to be updated.from:%s;to:%s", Level.FINE, ad.getFreeformTags(), tags); |
| 122 | + updateRequired = true; |
| 123 | + } |
| 124 | + if (definedTags != null && definedTags.entrySet().size() > 0 && !definedTags.equals(ad.getDefinedTags())) { |
| 125 | + reqBuilder = reqBuilder.definedTags(definedTags); |
| 126 | + debugLog(LOGGER, "Defined tags to be updated.from:%s;to:%s", Level.FINE, ad.getDefinedTags(), definedTags); |
| 127 | + updateRequired = true; |
| 128 | + } |
| 129 | + if (!updateRequired) { |
| 130 | + throw new AutonomousDatabaseAdapter.UpdateNotRequiredException(); |
| 131 | + } |
| 132 | + |
| 133 | + UpdateAutonomousDatabaseDetails request = reqBuilder.build(); |
| 134 | + UpdateAutonomousDatabaseResponse response = ociDBClient.updateAutonomousDatabase |
| 135 | + (UpdateAutonomousDatabaseRequest.builder().autonomousDatabaseId(adOCID) |
| 136 | + .updateAutonomousDatabaseDetails(request).build()); |
| 137 | + return buildADInstance(response.getAutonomousDatabase()); |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Get details of an AD instance. |
| 142 | + * @param adOCID OCID of the AD instance to be updated. |
| 143 | + * @return AutonomousDatabase |
| 144 | + */ |
| 145 | + public AutonomousDatabaseInstance get(String adOCID) { |
| 146 | + return buildADInstance(getADInstance(adOCID)); |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * Delete an AD instance. The operation will be asynchronously. |
| 151 | + * |
| 152 | + * @param adOCID OCID of the AD instance to be deleted. |
| 153 | + */ |
| 154 | + public void delete(String adOCID) { |
| 155 | + DeleteAutonomousDatabaseRequest request = DeleteAutonomousDatabaseRequest.builder().autonomousDatabaseId |
| 156 | + (adOCID).build(); |
| 157 | + ociDBClient.deleteAutonomousDatabase(request); |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * Update DB ADMIN password. |
| 162 | + * |
| 163 | + * @param password new DB ADMIN password. |
| 164 | + * @return AutonomousDatabase |
| 165 | + */ |
| 166 | + public AutonomousDatabaseInstance changePassword(String password) { |
| 167 | + UpdateAutonomousDatabaseDetails request = UpdateAutonomousDatabaseDetails.builder().adminPassword(password) |
| 168 | + .build(); |
| 169 | + UpdateAutonomousDatabaseResponse response = ociDBClient.updateAutonomousDatabase |
| 170 | + (UpdateAutonomousDatabaseRequest.builder().updateAutonomousDatabaseDetails(request).build()); |
| 171 | + return buildADInstance(response.getAutonomousDatabase()); |
| 172 | + } |
| 173 | + |
| 174 | + /** |
| 175 | + * Download the credential/configuration files for connecting to an AD |
| 176 | + * instance. The files are base64 encoded and converted as strings. |
| 177 | + * |
| 178 | + * @param adID OCID of the AD instance. |
| 179 | + * @param dbName name of the database. |
| 180 | + * @param wPassword password to set for the Oracle wallet that is |
| 181 | + * created for this request. |
| 182 | + * @return Map with filename/attribute name as keys and filename/attribute |
| 183 | + * base64 encoded contents as values. |
| 184 | + * @throws IOException if downloading credential zip file fails. |
| 185 | + */ |
| 186 | + public Map<String, String> getCredentials(String adID, String dbName, String wPassword) throws IOException { |
| 187 | + GenerateAutonomousDatabaseWalletDetails adbWalletDetails = GenerateAutonomousDatabaseWalletDetails.builder() |
| 188 | + .password(wPassword).build(); |
| 189 | + GenerateAutonomousDatabaseWalletResponse adbWalletResponse = ociDBClient.generateAutonomousDatabaseWallet( |
| 190 | + GenerateAutonomousDatabaseWalletRequest.builder() |
| 191 | + .generateAutonomousDatabaseWalletDetails(adbWalletDetails) |
| 192 | + .autonomousDatabaseId(adID) |
| 193 | + .build()); |
| 194 | + return ADBUtils.generateCredentialsMap(dbName, adbWalletResponse.getInputStream()); |
| 195 | + } |
| 196 | + |
| 197 | + /** |
| 198 | + * Fetch the list of all AD with a specific display name in a given OCI |
| 199 | + * compartment. |
| 200 | + * |
| 201 | + * @param compartmentId OCID of the Compartment. |
| 202 | + * @param displayName display name to filter the AD instances. |
| 203 | + * @return List of ADB instance details. |
| 204 | + */ |
| 205 | + public List<AutonomousDatabaseInstance> listInstances(String compartmentId, String displayName) { |
| 206 | + ListAutonomousDatabasesRequest.Builder reqBuilder = ListAutonomousDatabasesRequest.builder().compartmentId |
| 207 | + (compartmentId); |
| 208 | + if (displayName != null && !displayName.isEmpty()) { |
| 209 | + reqBuilder.displayName(displayName); |
| 210 | + } |
| 211 | + ListAutonomousDatabasesRequest request = reqBuilder.build(); |
| 212 | + ListAutonomousDatabasesResponse response = ociDBClient.listAutonomousDatabases(request); |
| 213 | + List<AutonomousDatabaseInstance> autonomousDatabaseInstanceList = new ArrayList<>(); |
| 214 | + response.getItems().forEach((adwSummary) -> autonomousDatabaseInstanceList.add(buildADInstance(adwSummary))); |
| 215 | + |
| 216 | + return autonomousDatabaseInstanceList; |
| 217 | + } |
11 | 218 |
|
12 |
| -public interface AutonomousDatabaseOCIClient extends AutoCloseable { |
13 |
| - AutonomousDatabaseInstance create(String displayName, String dbName, int cpuCount, int StorageSize, Map<String, |
14 |
| - String> tags, Map<String, Map<String, Object>> definedTags, String password, boolean licenseIncluded); |
| 219 | + public void close() { |
| 220 | + ociDBClient.close(); |
| 221 | + } |
15 | 222 |
|
16 |
| - AutonomousDatabaseInstance update(String instanceOCID, String displayName, int cpuCount, int StorageSize, |
17 |
| - Map<String, String> tags, Map<String, Map<String, Object>> definedTags); |
| 223 | + private AutonomousDatabase getADInstance(String adOCID) { |
| 224 | + GetAutonomousDatabaseRequest request = GetAutonomousDatabaseRequest.builder().autonomousDatabaseId(adOCID) |
| 225 | + .build(); |
| 226 | + GetAutonomousDatabaseResponse response = ociDBClient.getAutonomousDatabase(request); |
| 227 | + return response.getAutonomousDatabase(); |
| 228 | + } |
18 | 229 |
|
19 |
| - AutonomousDatabaseInstance get(String instanceOCID); |
| 230 | + private AutonomousDatabaseInstance buildADInstance(AutonomousDatabaseSummary summary) { |
| 231 | + if (summary == null) { |
| 232 | + return null; |
| 233 | + } else { |
| 234 | + return new AutonomousDatabaseInstance(summary.getId(), |
| 235 | + getDBWorkloadType(summary.getDbWorkload().getValue()), |
| 236 | + summary.getDisplayName(), |
| 237 | + summary.getCpuCoreCount(), |
| 238 | + summary.getDataStorageSizeInTBs(), |
| 239 | + summary.getDbName(), |
| 240 | + getADBLicenseType(summary.getLicenseModel()), |
| 241 | + summary.getFreeformTags(), |
| 242 | + AutonomousDatabaseInstance.lifecycleState(summary.getLifecycleState().getValue())); |
| 243 | + } |
| 244 | + } |
20 | 245 |
|
21 |
| - void delete(String instanceOCID); |
| 246 | + private AutonomousDatabaseInstance buildADInstance(AutonomousDatabase adbInstance) { |
| 247 | + if (adbInstance == null) { |
| 248 | + return null; |
| 249 | + } else { |
| 250 | + return new AutonomousDatabaseInstance(adbInstance.getId(), |
| 251 | + getDBWorkloadType(adbInstance.getDbWorkload().getValue()), |
| 252 | + adbInstance.getDisplayName(), |
| 253 | + adbInstance.getCpuCoreCount(), |
| 254 | + adbInstance.getDataStorageSizeInTBs(), |
| 255 | + adbInstance.getDbName(), |
| 256 | + getADBLicenseType(adbInstance.getLicenseModel()), |
| 257 | + adbInstance.getFreeformTags(), |
| 258 | + AutonomousDatabaseInstance.lifecycleState(adbInstance.getLifecycleState().getValue())); |
| 259 | + } |
| 260 | + } |
22 | 261 |
|
23 |
| - void close(); |
| 262 | + private AutonomousDatabaseAdapter.DBWorkloadType getDBWorkloadType (String dbWorkloadType) { |
| 263 | + if (dbWorkloadType.equalsIgnoreCase(AutonomousDatabase.DbWorkload.Oltp.getValue())) { |
| 264 | + return AutonomousDatabaseAdapter.DBWorkloadType.ATP; |
| 265 | + } else { |
| 266 | + return AutonomousDatabaseAdapter.DBWorkloadType.ADW; |
| 267 | + } |
| 268 | + } |
24 | 269 |
|
25 |
| - Map<String, String> getCredentials(String atpId, String dbName, String wPassword) throws IOException; |
| 270 | + private AutonomousDatabaseAdapter.LicenseModel getADBLicenseType(AutonomousDatabaseSummary.LicenseModel |
| 271 | + sdkLicenseModel){ |
| 272 | + switch (sdkLicenseModel) { |
| 273 | + case BringYourOwnLicense: return AutonomousDatabaseAdapter.LicenseModel.BYOL; |
| 274 | + case LicenseIncluded: return AutonomousDatabaseAdapter.LicenseModel.NEW; |
| 275 | + default: return AutonomousDatabaseAdapter.LicenseModel.UNKNOWN; |
| 276 | + } |
| 277 | + } |
26 | 278 |
|
27 |
| - List<AutonomousDatabaseInstance> listInstances(String compartmentId, String displayName); |
| 279 | + private AutonomousDatabaseAdapter.LicenseModel getADBLicenseType(AutonomousDatabase.LicenseModel |
| 280 | + adwLicenseModel){ |
| 281 | + switch (adwLicenseModel) { |
| 282 | + case BringYourOwnLicense: return AutonomousDatabaseAdapter.LicenseModel.BYOL; |
| 283 | + case LicenseIncluded: return AutonomousDatabaseAdapter.LicenseModel.NEW; |
| 284 | + default: return AutonomousDatabaseAdapter.LicenseModel.UNKNOWN; |
| 285 | + } |
| 286 | + } |
28 | 287 | }
|
0 commit comments