/*
* Copyright 2010-2013, CloudBees Inc.
*
* Licensed 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 com.cloudbees.sdk.commands.bg;
import com.cloudbees.api.BeesClient;
import com.cloudbees.api.ConfigurationParametersResponse;
import com.cloudbees.api.config.ConfigParameters;
import com.cloudbees.api.config.ParameterMap;
import java.util.Arrays;
import java.util.List;
public class BlueGreenSettings {
private String application1;
private String application2;
private String activeAliases;
private String account;
protected BlueGreenSettings(String account, String str) {
int idx = str.indexOf('[');
if (idx < 0) throw new IllegalArgumentException("Invalid format: app1,app2[aliases]");
String apps = str.substring(0, idx);
int idx2 = str.indexOf(']');
if (idx2 < idx) throw new IllegalArgumentException("Invalid format: app1,app2[aliases]");
activeAliases = str.substring(idx + 1, idx2);
String[] parts = apps.split(",");
if (parts.length < 2) throw new IllegalArgumentException("Invalid format: app1,app2[aliases]");
this.account = account;
setApplication1(parts[0]);
setApplication2(parts[1]);
}
public String getApplication1() {
return application1;
}
protected void setApplication1(String application) {
String[] parts = application.split("/");
if (parts.length < 2) {
application = account + "/" + application;
}
this.application1 = application;
}
public String getApplication2() {
return application2;
}
protected void setApplication2(String application) {
String[] parts = application.split("/");
if (parts.length < 2) {
application = account + "/" + application;
}
this.application2 = application;
}
public List<String> getActiveAliases() {
String[] p = activeAliases.split(",");
return Arrays.asList(p);
}
public static BlueGreenSettings getInstance(BeesClient client, String account, String bgName) throws Exception {
ConfigurationParametersResponse res = client.configurationParameters(account, "global");
if (res.getConfiguration() != null) {
ConfigParameters configParameters = ConfigParameters.parse(res.getConfiguration());
ParameterMap parameterMap = configParameters.getParameters();
String param = parameterMap.get(".bg.deploy." + bgName);
if (param != null) {
return new BlueGreenSettings(account, param);
}
}
throw new IllegalArgumentException("Application not configured for Blue-Green deployment.");
}
public String getActiveAliasesString() {
return activeAliases;
}
}