Skip to content

Commit

Permalink
feat(alibabacloud): add a new provider of alicloud (spinnaker#3055)
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaozhu36 authored and robzienert committed Aug 1, 2019
1 parent 48e545b commit 4963dc2
Show file tree
Hide file tree
Showing 6 changed files with 287 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class BakeRequest {
}

static enum CloudProviderType {
aws, azure, docker, gce, openstack, titus, oracle
aws, azure, docker, gce, openstack, titus, oracle, alicloud
}

static enum VmType {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* Copyright 2019 Alibaba Group.
*
* 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.netflix.spinnaker.orca.clouddriver.tasks.providers.alicloud;

import com.netflix.spinnaker.orca.clouddriver.MortService;
import com.netflix.spinnaker.orca.clouddriver.MortService.SecurityGroup;
import com.netflix.spinnaker.orca.clouddriver.tasks.securitygroup.SecurityGroupUpserter;
import com.netflix.spinnaker.orca.clouddriver.utils.CloudProviderAware;
import com.netflix.spinnaker.orca.pipeline.model.Stage;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class AliCloudSecurityGroupUpserter implements SecurityGroupUpserter, CloudProviderAware {

final String cloudProvider = "alicloud";

@Autowired MortService mortService;

@Override
public OperationContext getOperationContext(Stage stage) {
SecurityGroupUpserter.OperationContext operationContext =
new SecurityGroupUpserter.OperationContext();
Map<String, Object> context = stage.getContext();
Map<String, Object> extraOutput = new HashMap(30);
String name = (String) context.get("securityGroupName");
String vpcId = (String) context.get("vpcId");
Object securityGroupIngress = context.get("securityGroupIngress");
List<Map> list = new ArrayList<>();
List<MortService.SecurityGroup> targets = new ArrayList<>();
List<String> regions = new ArrayList<>();
if (context.get("regions") != null) {
regions.addAll((List) context.get("regions"));
} else {
if (context.get("region") != null) {
regions.add((String) context.get("region"));
}
}
for (String region : regions) {
Map<String, Object> map = new HashMap(16);
Map<String, Object> operation = new HashMap(50);
MortService.SecurityGroup securityGroup = new MortService.SecurityGroup();
securityGroup.setAccountName(getCredentials(stage));
securityGroup.setRegion(region);
securityGroup.setName(name);
securityGroup.setVpcId(vpcId);
targets.add(securityGroup);
operation.putAll(context);
operation.put("region", region);
map.put(SecurityGroupUpserter.OPERATION, operation);
list.add(map);
}
extraOutput.put("targets", targets);
extraOutput.put("securityGroupIngress", securityGroupIngress);
operationContext.setOperations(list);
operationContext.setExtraOutput(extraOutput);
return operationContext;
}

@Override
public boolean isSecurityGroupUpserted(SecurityGroup upsertedSecurityGroup, Stage stage) {
if (upsertedSecurityGroup == null) {
return false;
}
SecurityGroup securityGroup =
mortService.getSecurityGroup(
upsertedSecurityGroup.getAccountName(),
cloudProvider,
upsertedSecurityGroup.getName(),
upsertedSecurityGroup.getRegion(),
upsertedSecurityGroup.getVpcId());

if (upsertedSecurityGroup.getName().equals(securityGroup.getName())) {
return true;
} else {
return false;
}
}

@Override
public String getCloudProvider() {
return cloudProvider;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright 2019 Alibaba Group.
*
* 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.netflix.spinnaker.orca.clouddriver.tasks.providers.alicloud;

import com.netflix.spinnaker.orca.clouddriver.tasks.servergroup.ServerGroupCreator;
import com.netflix.spinnaker.orca.pipeline.model.Stage;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.springframework.stereotype.Component;

@Component
public class AliCloudServerGroupCreator implements ServerGroupCreator {

final String cloudProvider = "alicloud";

@Override
public List<Map> getOperations(Stage stage) {
List<Map> list = new ArrayList<>();
Map<String, Object> map = new HashMap(16);
Map<String, Object> operation = new HashMap(50);
operation.putAll(stage.getContext());
if (stage.getContext().get("account") != null
&& stage.getContext().get("credentials") == null) {
operation.put("credentials", stage.getContext().get("account"));
}
map.put(ServerGroupCreator.OPERATION, operation);
list.add(map);
return list;
}

@Override
public boolean isKatoResultExpected() {
return false;
}

@Override
public String getCloudProvider() {
return cloudProvider;
}

@Override
public Optional<String> getHealthProviderName() {
return Optional.of("AliCloud");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright 2019 Alibaba Group.
*
* 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.netflix.spinnaker.orca.kato.pipeline;

import com.netflix.spinnaker.orca.kato.tasks.ModifyScalingGroupTask;
import com.netflix.spinnaker.orca.pipeline.StageDefinitionBuilder;
import com.netflix.spinnaker.orca.pipeline.TaskNode.Builder;
import com.netflix.spinnaker.orca.pipeline.model.Stage;
import groovy.transform.CompileStatic;
import javax.annotation.Nonnull;
import org.springframework.stereotype.Component;

@Component
@CompileStatic
public class ModifyScalingGroupStage implements StageDefinitionBuilder {

@Override
public void taskGraph(@Nonnull Stage stage, @Nonnull Builder builder) {
builder.withTask("modifyScalingGroup", ModifyScalingGroupTask.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright 2019 Alibaba Group.
*
* 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.netflix.spinnaker.orca.kato.pipeline.support;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ScalingGroupDescriptionSupport {

public static Map convertScalingGroupToDeploymentTargets(
List<Map<String, String>> scalingGroups) {
Map<String, Object> deployServerGroups = new HashMap<>(16);
for (Map<String, String> scalingGroup : scalingGroups) {
deployServerGroups.put(scalingGroup.get("region"), scalingGroup.get("scalingGroupName"));
}
return deployServerGroups;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright 2019 Alibaba Group.
*
* 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.netflix.spinnaker.orca.kato.tasks;

import com.google.common.collect.ImmutableMap;
import com.netflix.spinnaker.orca.ExecutionStatus;
import com.netflix.spinnaker.orca.Task;
import com.netflix.spinnaker.orca.TaskResult;
import com.netflix.spinnaker.orca.clouddriver.KatoService;
import com.netflix.spinnaker.orca.kato.pipeline.support.ScalingGroupDescriptionSupport;
import com.netflix.spinnaker.orca.pipeline.model.Stage;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Nonnull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class ModifyScalingGroupTask implements Task {

@Autowired KatoService kato;

@Nonnull
@Override
public TaskResult execute(@Nonnull Stage stage) {
List list = new ArrayList<>();
Map<String, Map> objectMap = new HashMap<>(16);
Map<String, Object> context = stage.getContext();
objectMap.put("modifyScalingGroupDescription", context);
list.add(objectMap);
Object taskId = kato.requestOperations(list).toBlocking().first();
List<Map<String, String>> scalingGroups = (List) context.get("scalingGroups");
Map deployServerGroups =
ScalingGroupDescriptionSupport.convertScalingGroupToDeploymentTargets(scalingGroups);
Map<String, Object> result =
new ImmutableMap.Builder<String, Object>()
.put("notification.type", "modifyScalingGroup")
.put("deploy.server.groups", deployServerGroups)
.put("kato.last.task.id", taskId)
.build();
return TaskResult.builder(ExecutionStatus.SUCCEEDED).context(result).build();
}
}

0 comments on commit 4963dc2

Please sign in to comment.