|
| 1 | +package com.dtsx.astra.sdk.db; |
| 2 | + |
| 3 | +import com.dtsx.astra.sdk.AbstractApiClient; |
| 4 | +import com.dtsx.astra.sdk.db.domain.Database; |
| 5 | +import com.dtsx.astra.sdk.db.domain.DatabaseCloneStatus; |
| 6 | +import com.dtsx.astra.sdk.utils.ApiLocator; |
| 7 | +import com.dtsx.astra.sdk.utils.ApiResponseHttp; |
| 8 | +import com.dtsx.astra.sdk.utils.Assert; |
| 9 | +import com.dtsx.astra.sdk.utils.AstraEnvironment; |
| 10 | +import com.dtsx.astra.sdk.utils.JsonUtils; |
| 11 | + |
| 12 | +import java.net.HttpURLConnection; |
| 13 | +import java.net.URLEncoder; |
| 14 | +import java.nio.charset.StandardCharsets; |
| 15 | +import java.util.stream.Stream; |
| 16 | + |
| 17 | +/** |
| 18 | + * Delegate operation on DB cloning |
| 19 | + */ |
| 20 | +public class DbCloneClient extends AbstractApiClient { |
| 21 | + |
| 22 | + /** Clone path component, part I. */ |
| 23 | + public static final String PATH_CLONE_1 = "/databases/"; |
| 24 | + |
| 25 | + /** Clone path component, part II. */ |
| 26 | + public static final String PATH_CLONE_2 = "/cloneFrom/"; |
| 27 | + |
| 28 | + /** Get-clone-status path component, part I. */ |
| 29 | + public static final String PATH_GETSTATUS_1 = "/databases/"; |
| 30 | + |
| 31 | + /** Get-clone-status path component, part II. */ |
| 32 | + public static final String PATH_GETSTATUS_2 = "/cloneStatus/"; |
| 33 | + |
| 34 | + /** |
| 35 | + * unique db identifier. |
| 36 | + */ |
| 37 | + private final Database db; |
| 38 | + |
| 39 | + /** |
| 40 | + * As immutable object use builder to initiate the object. |
| 41 | + * |
| 42 | + * @param token |
| 43 | + * authenticated token |
| 44 | + * @param databaseId |
| 45 | + * database identifier |
| 46 | + */ |
| 47 | + public DbCloneClient(String token, String databaseId) { |
| 48 | + this(token, AstraEnvironment.PROD, databaseId); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * As immutable object use builder to initiate the object. |
| 53 | + * |
| 54 | + * @param env |
| 55 | + * define target environment to be used |
| 56 | + * @param token |
| 57 | + * authenticated token |
| 58 | + * @param databaseId |
| 59 | + * database identifier |
| 60 | + */ |
| 61 | + public DbCloneClient(String token, AstraEnvironment env, String databaseId) { |
| 62 | + super(token, env); |
| 63 | + Assert.hasLength(databaseId, "databaseId"); |
| 64 | + this.db = new DbOpsClient(token, env, databaseId).get(); |
| 65 | + } |
| 66 | + |
| 67 | + /** {@inheritDoc} */ |
| 68 | + @Override |
| 69 | + public String getServiceName() { |
| 70 | + return "db.clone"; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Clone database from a source database using a snapshot. |
| 75 | + * |
| 76 | + * @param sourceDbId |
| 77 | + * ID of the source database |
| 78 | + * @param databaseSnapshotId |
| 79 | + * ID of the snapshot to clone from |
| 80 | + * @return clone status |
| 81 | + */ |
| 82 | + public DatabaseCloneStatus cloneFrom(String sourceDbId, String databaseSnapshotId) { |
| 83 | + return cloneFrom(sourceDbId, databaseSnapshotId, null); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Clone database from a source database using a snapshot. |
| 88 | + * |
| 89 | + * @param sourceDbId |
| 90 | + * ID of the source database |
| 91 | + * @param databaseSnapshotId |
| 92 | + * ID of the snapshot to clone from |
| 93 | + * @param sourceRegion |
| 94 | + * Source region (optional) |
| 95 | + * @return clone status |
| 96 | + */ |
| 97 | + public DatabaseCloneStatus cloneFrom(String sourceDbId, String databaseSnapshotId, String sourceRegion) { |
| 98 | + Assert.hasLength(sourceDbId, "sourceDbId"); |
| 99 | + Assert.hasLength(databaseSnapshotId, "databaseSnapshotId"); |
| 100 | + |
| 101 | + // Build URL with query parameters |
| 102 | + String url = getEndpointCloneFrom(sourceDbId) + |
| 103 | + "?snapshotID=" + URLEncoder.encode(databaseSnapshotId, StandardCharsets.UTF_8); |
| 104 | + |
| 105 | + if (sourceRegion != null && !sourceRegion.isEmpty()) { |
| 106 | + url += "&sourceRegion=" + URLEncoder.encode(sourceRegion, StandardCharsets.UTF_8); |
| 107 | + } |
| 108 | + |
| 109 | + // Fire POST request |
| 110 | + ApiResponseHttp res = POST(url, getOperationName("cloneFrom")); |
| 111 | + |
| 112 | + // Parse and return response |
| 113 | + return JsonUtils.unmarshallBean(res.getBody(), DatabaseCloneStatus.class); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Get the status of a clone operation. |
| 118 | + * |
| 119 | + * @param operationId |
| 120 | + * ID of the clone operation |
| 121 | + * @return clone status |
| 122 | + */ |
| 123 | + public DatabaseCloneStatus getCloneStatus(String operationId) { |
| 124 | + Assert.hasLength(operationId, "operationId"); |
| 125 | + |
| 126 | + // Build URL |
| 127 | + String url = getEndpointGetCloneStatus(operationId); |
| 128 | + |
| 129 | + // Fire GET request |
| 130 | + ApiResponseHttp res = GET(url, getOperationName("getCloneStatus")); |
| 131 | + |
| 132 | + // Parse and return response |
| 133 | + return JsonUtils.unmarshallBean(res.getBody(), DatabaseCloneStatus.class); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Endpoint to initiate DB clone |
| 138 | + * |
| 139 | + * @param sourceDbId |
| 140 | + * ID of the source database |
| 141 | + * @return request endpoint |
| 142 | + */ |
| 143 | + private String getEndpointCloneFrom(String sourceDbId) { |
| 144 | + return ApiLocator.getApiDevopsEndpoint(environment) + PATH_CLONE_1 + db.getId() + PATH_CLONE_2 + sourceDbId; |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Endpoint to retrieve a DB-clone status (by operation ID) |
| 149 | + * |
| 150 | + * @param operationId |
| 151 | + * ID of the clone operation (as obtained e.g. with a cloneFrom previous invocation) |
| 152 | + * @return request endpoint |
| 153 | + */ |
| 154 | + private String getEndpointGetCloneStatus(String operationId) { |
| 155 | + return ApiLocator.getApiDevopsEndpoint(environment) + PATH_GETSTATUS_1 + db.getId() + PATH_GETSTATUS_2 + operationId; |
| 156 | + } |
| 157 | + |
| 158 | +} |
0 commit comments