Skip to content
This repository has been archived by the owner on Oct 28, 2024. It is now read-only.

Commit

Permalink
#143: Support shield v8 and v7 transparently
Browse files Browse the repository at this point in the history
  • Loading branch information
ampersand8 authored and seilc1 committed Sep 20, 2018
1 parent 03cd450 commit 4fbd305
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import spock.lang.IgnoreIf
import spock.lang.Shared

@Slf4j
@IgnoreIf({ !Boolean.valueOf(System.properties['com.swisscom.cloud.sb.broker.runMariaDBBackupRestoreFunctionalSpec']) })
//@IgnoreIf({ !Boolean.valueOf(System.properties['com.swisscom.cloud.sb.broker.runMariaDBBackupRestoreFunctionalSpec']) })
class MariaDBBackupRestoreFunctionalSpec extends BaseFunctionalSpec {

@Shared
Expand All @@ -58,7 +58,7 @@ class MariaDBBackupRestoreFunctionalSpec extends BaseFunctionalSpec {
serviceLifeCycler.createParameter('BACKUP_SCHEDULE_NAME', 'daily', serviceLifeCycler.plan)
serviceLifeCycler.createParameter('BACKUP_POLICY_NAME', 'month', serviceLifeCycler.plan)
serviceLifeCycler.createParameter('BACKUP_STORAGE_NAME', 'default', serviceLifeCycler.plan)
backupRestoreHelper = new BackupRestoreHelper(appBaseUrl, cfExtUser, cfExtPassword)
backupRestoreHelper = new BackupRestoreHelper(appBaseUrl, cfExtUser.username, cfExtUser.password)
}

@Autowired
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,16 @@ class ShieldClient {
if (retention == null) {
throw new RuntimeException("Retention ${shieldServiceConfig.retentionName} that is configured does not exist on shield")
}
ScheduleDto schedule = buildClient().getScheduleByName(shieldServiceConfig.scheduleName)
if (schedule == null) {
throw new RuntimeException("Schedule ${shieldServiceConfig.scheduleName} that is configured does not exist on shield")
String schedule = shieldServiceConfig.scheduleName
if (buildClient().getAPIVersion() == 1) {
ScheduleDto scheduleDto = buildClient().getScheduleByName(shieldServiceConfig.scheduleName)
if (scheduleDto == null) {
throw new RuntimeException("Schedule ${shieldServiceConfig.scheduleName} that is configured does not exist on shield")
}
schedule = scheduleDto.uuid
}

createOrUpdateJob(jobName, targetUuid, store.uuid, retention.uuid, schedule.uuid, paused)
createOrUpdateJob(jobName, targetUuid, store.uuid, retention.uuid, schedule, paused)
}

private String createOrUpdateTarget(ShieldTarget target, String targetName, String agent) {
Expand All @@ -188,6 +192,6 @@ class ShieldClient {
}

private ShieldRestClient buildClient() {
shieldRestClientFactory.build(restTemplateBuilder.withSSLValidationDisabled().build(), shieldConfig.baseUrl, shieldConfig.apiKey)
shieldRestClientFactory.build(restTemplateBuilder.withSSLValidationDisabled().build(), shieldConfig)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,7 @@ class ShieldConfig implements Config {
String jobPrefix
String targetPrefix
int maxRetryBackup
String defaultTenantName
String username
String password
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,48 @@ import com.swisscom.cloud.sb.broker.util.GsonFactory
import groovy.json.JsonSlurper
import groovy.util.logging.Slf4j
import org.springframework.http.*
import org.springframework.web.client.HttpStatusCodeException
import org.springframework.web.client.RestTemplate

@Slf4j
class ShieldRestClient {
public static final String HEADER_API_KEY = 'X-Shield-Token'
public static final String HEADER_API_SESSION = 'X-Shield-Session'

private ShieldConfig config
private RestTemplate restTemplate
private String baseUrl
private String apiKey
private int apiVersion = 1

ShieldRestClient(RestTemplate restTemplate, String baseUrl, String apiKey) {
ShieldRestClient(RestTemplate restTemplate, ShieldConfig shieldConfig) {
this.restTemplate = restTemplate
this.baseUrl = baseUrl
this.apiKey = apiKey
this.config = shieldConfig
this.apiVersion = getAPIVersion()
}

int getAPIVersion() {
try {
this.apiVersion = 1
def response = restTemplate.exchange(statusUrl(), HttpMethod.GET, configureRequestEntity(), String.class)
String version = new JsonSlurper().parseText(response.body).version
if (version != null && version[0..0].toInteger() == 1) return 1
else {
this.apiVersion = 2
response = restTemplate.exchange(infoUrl(), HttpMethod.GET, configureRequestEntity(), String.class)
if (new JsonSlurper().parseText(response.body).api == 2) return 2
}
} catch (HttpStatusCodeException e) {
if (e.getStatusCode() == HttpStatus.NOT_FOUND) {
this.apiVersion = 2
def response = restTemplate.exchange(infoUrl(), HttpMethod.GET, configureRequestEntity(), String.class)
if (new JsonSlurper().parseText(response.body).api == 2) return 2
throw e
}
}
}

String getTenantUuidByName(String name) {
def response = restTemplate.exchange(tenantsUrl() + "?limit=1&name=${name}", HttpMethod.GET, configureRequestEntity(), String.class)
return new JsonSlurper().parseText(response.body)[0].uuid
}

Object getStatus() {
Expand Down Expand Up @@ -149,6 +177,9 @@ class ShieldRestClient {
schedule : scheduleUuid,
paused : paused]

if (apiVersion == 2) {
body = replaceRetentionWithPolicy(body, retentionUuid)
}
def response = restTemplate.exchange(jobsUrl(), HttpMethod.POST, configureRequestEntity(body), String.class)
new JsonSlurper().parseText(response.body).uuid
}
Expand All @@ -166,7 +197,9 @@ class ShieldRestClient {
retention: retentionUuid,
schedule : scheduleUuid,
paused : paused]

if (apiVersion == 2) {
body = replaceRetentionWithPolicy(body, retentionUuid)
}
restTemplate.exchange(jobUrl(existingJob.uuid), HttpMethod.PUT, configureRequestEntity(body), (Class) null)
existingJob.uuid
}
Expand Down Expand Up @@ -218,52 +251,89 @@ class ShieldRestClient {
private <T> HttpEntity<T> configureRequestEntity(T t) {
HttpHeaders headers = new HttpHeaders()
headers.setContentType(MediaType.valueOf(MediaType.APPLICATION_JSON_VALUE))
headers.add(HEADER_API_KEY, apiKey)
apiVersion == 2 ? headers.add(HEADER_API_SESSION, getSession()) : headers.add(HEADER_API_KEY, config.apiKey)
HttpEntity<T> entity = t ? new HttpEntity<T>(t, headers) : new HttpEntity<T>(headers)
return entity
}

protected String getSession() {
HttpHeaders headers = new HttpHeaders()
headers.setContentType(MediaType.valueOf(MediaType.APPLICATION_JSON_VALUE))
def body = [username: config.username,
password: config.password]
HttpEntity<Map<String,String>> request = new HttpEntity<Map<String,String>>(body, headers)
ResponseEntity<String> response = restTemplate.exchange(loginUrl(), HttpMethod.POST, request, String.class)
return response.getHeaders().getValuesAsList(HEADER_API_SESSION)[0]
}

protected String storesUrl() {
"${baseUrl()}/stores"
"${tenantBasedUrl()}/stores"
}

protected String retentionsUrl() {
"${baseUrl()}/retention"
apiVersion == 2 ? "${tenantBasedUrl()}/policies" : "${tenantBasedUrl()}/retention"
}

protected String schedulesUrl() {
"${baseUrl()}/schedules"
"${tenantBasedUrl()}/schedules"
}

protected String taskUrl(String uuid) {
"${baseUrl()}/task/${uuid}"
"${tenantBasedUrl()}/task/${uuid}"
}

protected String archiveUrl(String uuid) {
"${baseUrl()}/archive/${uuid}"
"${tenantBasedUrl()}/archive/${uuid}"
}

protected String targetsUrl() {
"${baseUrl()}/targets"
"${tenantBasedUrl()}/targets"
}

protected String targetUrl(String uuid) {
"${baseUrl()}/target/${uuid}"
"${tenantBasedUrl()}/target/${uuid}"
}

protected String jobsUrl() {
"${baseUrl()}/jobs"
"${tenantBasedUrl()}/jobs"
}

protected String jobUrl(String uuid) {
"${baseUrl()}/job/${uuid}"
"${tenantBasedUrl()}/job/${uuid}"
}

protected String statusUrl() {
"${baseUrl()}/status"
}

protected String infoUrl() {
"${baseUrl()}/info"
}

protected String tenantsUrl() {
"${baseUrl()}/tenants"
}

protected String loginUrl() {
"${baseUrl()}/auth/login"
}

private String tenantBasedUrl() {
apiVersion == 2 ? "${baseUrl()}/tenants/${getTenantUuidByName(config.defaultTenantName)}" : baseUrl()
}

private String baseUrl() {
"${baseUrl}/v1"
"${config.baseUrl}/v${apiVersion}"
}

private Map<String,String> replaceRetentionWithPolicy(Map<?, ?> body, String retention) {
def iterator = body.entrySet().iterator()
while (iterator.hasNext()) {
if (iterator.next().key == "retention") {
iterator.remove()
}
}
body.put("policy", retention)
return body
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import org.springframework.web.client.RestTemplate
@Component
@CompileStatic
class ShieldRestClientFactory {
ShieldRestClient build(RestTemplate restTemplate, String baseUrl, String apiKey) {
ShieldRestClient build(RestTemplate restTemplate, ShieldConfig shieldConfig) {
restTemplate.setErrorHandler(new ShieldRestResponseErrorHandler())
new ShieldRestClient(restTemplate, baseUrl, apiKey)
new ShieldRestClient(restTemplate, shieldConfig)
}
}
5 changes: 4 additions & 1 deletion broker/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ com.swisscom.cloud.sb.broker.service.mariadb:
discoveryURL: "http://localhost:8080/v2/api-docs"

com.swisscom.cloud.sb.broker.shield:
baseUrl: 'https://shield.service.consul:8002'
baseUrl: 'https://localhost:8443'
apiKey:
agent: '10.244.2.2:5444'
jobPrefix: 'SB_CF_'
Expand All @@ -119,6 +119,9 @@ com.swisscom.cloud.sb.broker.shield:
retentionName: 'default'
scheduleName: 'schedu'
maxRetryBackup: 5
defaultTenantName: tenant1
username: admin
password: shield

com.swisscom.cloud.sb.broker.service.openwhisk:
openWhiskProtocol:
Expand Down
Loading

0 comments on commit 4fbd305

Please sign in to comment.