From 312652ff2e850a1f7b9a3e852d209725aa47c90a Mon Sep 17 00:00:00 2001 From: "zigang.wang" Date: Wed, 15 Jun 2022 10:14:26 +0800 Subject: [PATCH 1/2] feat(alicloud): cluster view MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - refactor alicloud code - Supports cluster、application、loadbalancer view --- .../clouddriver/alicloud/cache/Keys.java | 25 +- .../alicloud/common/CacheDataHelper.java | 51 ++ .../alicloud/common/ClientFactory.java | 7 +- .../alicloud/common/DateParseHelper.java | 43 ++ .../alicloud/common/HealthHelper.java | 99 ++-- .../clouddriver/alicloud/common/Sets.java | 28 + .../controllers/AliCloudImageController.java | 51 +- .../AliCloudScalingActivitiesController.java | 3 +- .../alicloud/model/AliCloudApplication.java | 54 ++ .../alicloud/model/AliCloudCluster.java | 6 +- .../alicloud/model/AliCloudInstance.java | 26 +- .../alicloud/model/AliCloudKeyPair.java | 26 +- .../alicloud/model/AliCloudLoadBalancer.java | 53 +- .../alicloud/model/AliCloudSecurityGroup.java | 37 +- .../alicloud/model/AliCloudServerGroup.java | 19 +- .../alicloud/model/AliCloudSubnet.java | 6 +- .../model/alienum/LifecycleState.java | 23 + .../alicloud/model/alienum/ListenerType.java | 15 +- .../alienum/ScalingInstanceHealthStatus.java | 35 ++ .../alicloud/provider/AliProvider.java | 26 +- .../alicloud/provider/AliProviderConfig.java | 39 +- .../agent/AliCloudClusterCachingAgent.java | 488 ++++++++++++++++++ .../agent/AliCloudImageCachingAgent.java | 158 ++++++ .../agent/AliCloudInstanceCachingAgent.java | 133 +++++ .../AliCloudInstanceTypeCachingAgent.java | 155 ++++++ .../agent/AliCloudKeyPairCachingAgent.java | 123 +++++ .../AliCloudLoadBalancerCachingAgent.java | 383 ++++++++------ ...LoadBalancerInstanceStateCachingAgent.java | 76 +-- .../AliCloudSecurityGroupCachingAgent.java | 32 +- .../agent/AliCloudSubnetCachingAgent.java | 153 ++++++ .../view/AliCloudApplicationProvider.java | 101 ++++ .../view/AliCloudClusterProvider.java | 369 +++++++++++++ .../view/AliCloudInstanceProvider.java | 102 ++++ .../view/AliCloudInstanceTypeProvider.java | 101 ++++ .../view/AliCloudKeyPairProvider.java | 70 +++ .../view/AliCloudLoadBalancerProvider.java | 185 +++---- .../view/AliCloudSecurityGroupProvider.java | 17 +- .../provider/view/AliCloudSubnetProvider.java | 67 +++ .../clouddriver/alicloud/BaseTest.java | 46 ++ .../AliCloudClusterCachingAgentTest.java | 177 +++++++ .../agent/AliCloudImageCachingAgentTest.java | 78 +++ .../AliCloudInstanceTypeCachingAgentTest.java | 70 +++ .../AliCloudKeyPairCachingAgentTest.java | 71 +++ .../AliCloudLoadBalancerCachingAgentTest.java | 2 +- ...AliCloudSecurityGroupCachingAgentTest.java | 2 +- .../agent/AliCloudSubnetCachingAgentTest.java | 85 +++ .../agent/CommonCachingAgentTest.java | 8 +- .../view/AliCloudClusterProviderTest.java | 170 ++++++ .../AliCloudInstanceTypeProviderTest.java | 80 +++ .../view/AliCloudKeyPairProviderTest.java | 76 +++ .../AliCloudLoadBalancerProviderTest.java | 122 +++-- .../AliCloudSecurityGroupProviderTest.java | 12 +- .../view/AliCloudSubnetProviderTest.java | 80 +++ .../provider/view/CommonProvider.java | 8 +- ...describeLoadBalancerAttributeResponse.json | 56 ++ ...describeScalingConfigurationsResponse.json | 0 .../resources/mock/listenerAttributes.json | 20 + .../src/test/resources/mock/loadbalancer.json | 32 ++ .../src/test/resources/mock/scalingGroup.json | 0 .../test/resources/mock/scalingInstance.json | 0 .../src/test/resources/mock/vserverGroup.json | 8 + 61 files changed, 4005 insertions(+), 583 deletions(-) create mode 100644 clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/common/CacheDataHelper.java create mode 100644 clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/common/DateParseHelper.java create mode 100644 clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/common/Sets.java create mode 100644 clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/model/AliCloudApplication.java create mode 100644 clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/model/alienum/LifecycleState.java create mode 100644 clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/model/alienum/ScalingInstanceHealthStatus.java create mode 100644 clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/provider/agent/AliCloudClusterCachingAgent.java create mode 100644 clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/provider/agent/AliCloudImageCachingAgent.java create mode 100644 clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/provider/agent/AliCloudInstanceCachingAgent.java create mode 100644 clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/provider/agent/AliCloudInstanceTypeCachingAgent.java create mode 100644 clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/provider/agent/AliCloudKeyPairCachingAgent.java create mode 100644 clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/provider/agent/AliCloudSubnetCachingAgent.java create mode 100644 clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/provider/view/AliCloudApplicationProvider.java create mode 100644 clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/provider/view/AliCloudClusterProvider.java create mode 100644 clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/provider/view/AliCloudInstanceProvider.java create mode 100644 clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/provider/view/AliCloudInstanceTypeProvider.java create mode 100644 clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/provider/view/AliCloudKeyPairProvider.java create mode 100644 clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/provider/view/AliCloudSubnetProvider.java create mode 100644 clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/BaseTest.java create mode 100644 clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/provider/agent/AliCloudClusterCachingAgentTest.java create mode 100644 clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/provider/agent/AliCloudImageCachingAgentTest.java create mode 100644 clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/provider/agent/AliCloudInstanceTypeCachingAgentTest.java create mode 100644 clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/provider/agent/AliCloudKeyPairCachingAgentTest.java create mode 100644 clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/provider/agent/AliCloudSubnetCachingAgentTest.java create mode 100644 clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/provider/view/AliCloudClusterProviderTest.java create mode 100644 clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/provider/view/AliCloudInstanceTypeProviderTest.java create mode 100644 clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/provider/view/AliCloudKeyPairProviderTest.java create mode 100644 clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/provider/view/AliCloudSubnetProviderTest.java create mode 100644 clouddriver-alicloud/src/test/resources/mock/describeLoadBalancerAttributeResponse.json create mode 100644 clouddriver-alicloud/src/test/resources/mock/describeScalingConfigurationsResponse.json create mode 100644 clouddriver-alicloud/src/test/resources/mock/listenerAttributes.json create mode 100644 clouddriver-alicloud/src/test/resources/mock/loadbalancer.json create mode 100644 clouddriver-alicloud/src/test/resources/mock/scalingGroup.json create mode 100644 clouddriver-alicloud/src/test/resources/mock/scalingInstance.json create mode 100644 clouddriver-alicloud/src/test/resources/mock/vserverGroup.json diff --git a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/cache/Keys.java b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/cache/Keys.java index 4ce9de2136e..643cf3f41b5 100644 --- a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/cache/Keys.java +++ b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/cache/Keys.java @@ -22,6 +22,7 @@ import com.netflix.frigga.Names; import com.netflix.spinnaker.clouddriver.alicloud.model.AliCloudServerGroup; import com.netflix.spinnaker.clouddriver.cache.KeyParser; +import com.netflix.spinnaker.clouddriver.core.provider.agent.Namespace; import java.util.HashMap; import java.util.Map; import org.apache.commons.lang3.StringUtils; @@ -292,15 +293,33 @@ public static Map parse(String key) { result.put("region", parts[3]); result.put("account", parts[2]); result.put("vpcId", parts[6]); + return result; + } else { + return null; + } + case "clusters": + if (parts.length >= 5) { + result.put("application", parts[2]); + result.put("account", parts[3]); + result.put("clusterName", parts[4]); + return result; + } else { + return null; + } + + case "health": + if (parts.length >= 8) { + result.put("loadBalancerId", parts[2]); + result.put("instanceId", parts[3]); + result.put("port", parts[4]); + result.put("account", parts[5]); + return result; } else { return null; } - break; default: return null; } - - return result; } @Override diff --git a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/common/CacheDataHelper.java b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/common/CacheDataHelper.java new file mode 100644 index 00000000000..725f10698f7 --- /dev/null +++ b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/common/CacheDataHelper.java @@ -0,0 +1,51 @@ +/* + * Copyright 2022 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.clouddriver.alicloud.common; + +import com.netflix.spinnaker.cats.cache.CacheData; +import java.util.Map; +import org.jetbrains.annotations.Nullable; + +public class CacheDataHelper { + @Nullable + public static CacheData merge(CacheData o1, CacheData o2) { + if (o2 == null) { + return o1; + } + if (o1 == null) { + return o1; + } + + if (!o1.getId().equals(o2.getId())) { + throw new RuntimeException("Different ID cannot merge"); + } + + Map attributes = o1.getAttributes(); + attributes.putAll(o2.getAttributes()); + o2.getRelationships() + .forEach( + (k, v) -> { + if (o1.getRelationships().containsKey(k)) { + o1.getRelationships().get(k).addAll(v); + } else { + o1.getRelationships().put(k, v); + } + }); + o1.getRelationships().forEach((k, v) -> v.addAll(o2.getRelationships().get(k))); + return o1; + } +} diff --git a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/common/ClientFactory.java b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/common/ClientFactory.java index 238d5c8a541..946b23bc7ea 100644 --- a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/common/ClientFactory.java +++ b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/common/ClientFactory.java @@ -18,15 +18,16 @@ import com.aliyuncs.DefaultAcsClient; import com.aliyuncs.IAcsClient; +import com.aliyuncs.auth.AlibabaCloudCredentialsProvider; import com.aliyuncs.profile.DefaultProfile; import org.springframework.stereotype.Component; @Component public class ClientFactory { - public IAcsClient createClient(String region, String accessKeyId, String accessSecretKey) { - DefaultProfile profile = DefaultProfile.getProfile(region, accessKeyId, accessSecretKey); - DefaultAcsClient defaultAcsClient = new DefaultAcsClient(profile); + public IAcsClient createClient(String region, AlibabaCloudCredentialsProvider credentials) { + DefaultProfile profile = DefaultProfile.getProfile(region); + DefaultAcsClient defaultAcsClient = new DefaultAcsClient(profile, credentials); defaultAcsClient.appendUserAgent("Spinnaker", "Clouddriver v1.0"); return defaultAcsClient; } diff --git a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/common/DateParseHelper.java b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/common/DateParseHelper.java new file mode 100644 index 00000000000..a217f9303ba --- /dev/null +++ b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/common/DateParseHelper.java @@ -0,0 +1,43 @@ +/* + * Copyright 2022 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.clouddriver.alicloud.common; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; + +public class DateParseHelper { + static SimpleDateFormat YYYY_MM_DD_HH_MM_SS_UTC = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'"); + + public static Date parseUTCTime(String utcTime) throws ParseException { + if (utcTime == null || utcTime.length() == 0) { + return null; + } + + if (utcTime.endsWith("UTC")) { + utcTime = utcTime.replace("Z", " UTC"); + } + return YYYY_MM_DD_HH_MM_SS_UTC.parse(utcTime); + } + + public static String format(Date date) { + if (date == null) { + return null; + } + return YYYY_MM_DD_HH_MM_SS_UTC.format(date); + } +} diff --git a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/common/HealthHelper.java b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/common/HealthHelper.java index afe2cd30ef0..f70c7769d88 100644 --- a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/common/HealthHelper.java +++ b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/common/HealthHelper.java @@ -16,57 +16,43 @@ package com.netflix.spinnaker.clouddriver.alicloud.common; -import static com.netflix.spinnaker.clouddriver.core.provider.agent.Namespace.HEALTH; - -import com.netflix.spinnaker.cats.cache.Cache; import com.netflix.spinnaker.cats.cache.CacheData; -import com.netflix.spinnaker.clouddriver.alicloud.AliCloudProvider; +import com.netflix.spinnaker.clouddriver.alicloud.cache.Keys; +import com.netflix.spinnaker.clouddriver.alicloud.model.alienum.LifecycleState; +import com.netflix.spinnaker.clouddriver.alicloud.model.alienum.ScalingInstanceHealthStatus; import com.netflix.spinnaker.clouddriver.model.HealthState; -import java.util.*; -import java.util.regex.Pattern; -import java.util.stream.Collectors; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; import org.apache.commons.lang3.StringUtils; import org.springframework.util.CollectionUtils; public class HealthHelper { - private static boolean healthyStateMatcher(String key, String loadBalancerId, String instanceId) { - String regex; - if (StringUtils.isNotBlank(loadBalancerId)) { - regex = AliCloudProvider.ID + ":.*:" + loadBalancerId + ":" + instanceId + ":.*"; - return Pattern.matches(regex, key); - } else { - regex = AliCloudProvider.ID + ":.*:" + instanceId + ":.*"; + public static boolean healthyStateMatcher( + String key, Set loadBalancerId, String instanceId) { + + if (StringUtils.isBlank(key)) { + return false; } - return Pattern.matches(regex, key); + + Map details = Keys.parse(key); + if (details == null || details.isEmpty()) { + return false; + } + + boolean instanceMatch = StringUtils.equals(instanceId, details.get("instanceId")); + if (loadBalancerId == null || loadBalancerId.isEmpty()) { + return instanceMatch; + } + return loadBalancerId.contains(details.get("loadBalancerId")); } - public static HealthState judgeInstanceHealthyState( - Collection allHealthyKeys, - List loadBalancerIds, - String instanceId, - Cache cacheView) { - Set healthyKeys = new HashSet<>(); - if (loadBalancerIds != null) { - for (String loadBalancerId : loadBalancerIds) { - List collect = - allHealthyKeys.stream() - .filter(tab -> HealthHelper.healthyStateMatcher(tab, loadBalancerId, instanceId)) - .collect(Collectors.toList()); - Collection healthData = cacheView.getAll(HEALTH.ns, collect, null); - if (CollectionUtils.isEmpty(healthData)) { - return HealthState.Unknown; - } - healthyKeys.addAll(collect); - } - } else { - List collect = - allHealthyKeys.stream() - .filter(tab -> HealthHelper.healthyStateMatcher(tab, null, instanceId)) - .collect(Collectors.toList()); - healthyKeys.addAll(collect); + private static HealthState judgeInstanceHealthyState(Collection healthData) { + if (CollectionUtils.isEmpty(healthData)) { + return HealthState.Unknown; } - Collection healthData = cacheView.getAll(HEALTH.ns, healthyKeys, null); Map healthMap = new HashMap<>(16); for (CacheData cacheData : healthData) { String serverHealthStatus = cacheData.getAttributes().get("serverHealthStatus").toString(); @@ -84,4 +70,37 @@ public static HealthState judgeInstanceHealthyState( return HealthState.Unknown; } } + + public static ScalingInstanceHealthStatus loadBalancerInstanceHealthState( + String scalingGroupLifecycleState, + String scalingInstanceInstanceHealthStatus, + Collection healthData) { + if (!LifecycleState.Active.name().equals(scalingGroupLifecycleState)) { + return ScalingInstanceHealthStatus.Unhealthy; + } + + if (!ScalingInstanceHealthStatus.Healthy.name().equals(scalingInstanceInstanceHealthStatus)) { + return ScalingInstanceHealthStatus.Unhealthy; + } + HealthState healthState = judgeInstanceHealthyState(healthData); + return ScalingInstanceHealthStatus.forState(healthState); + } + + public static HealthState genInstanceHealthState( + String instanceStatus, Collection healthData) { + return "Running".equals(instanceStatus) + ? judgeInstanceHealthyState(healthData) + : HealthState.Down; + } + + public static HealthState genInstanceHealthState( + String scalingGroupLifecycleState, String instanceStatus, Collection healthData) { + if (!LifecycleState.Active.name().equals(scalingGroupLifecycleState)) { + return HealthState.Down; + } + if (!"Healthy".equals(instanceStatus)) { + return HealthState.Down; + } + return HealthHelper.judgeInstanceHealthyState(healthData); + } } diff --git a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/common/Sets.java b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/common/Sets.java new file mode 100644 index 00000000000..ba80531ce2c --- /dev/null +++ b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/common/Sets.java @@ -0,0 +1,28 @@ +/* + * Copyright 2022 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.clouddriver.alicloud.common; + +import java.util.HashSet; +import java.util.Set; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +public class Sets { + public static Set ofModifiable(String... arr) { + return Stream.of(arr).collect(Collectors.toCollection(HashSet::new)); + } +} diff --git a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/controllers/AliCloudImageController.java b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/controllers/AliCloudImageController.java index 41959022de5..f6f8c992f15 100644 --- a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/controllers/AliCloudImageController.java +++ b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/controllers/AliCloudImageController.java @@ -24,9 +24,13 @@ import com.netflix.spinnaker.clouddriver.alicloud.cache.Keys; import com.netflix.spinnaker.kork.web.exceptions.InvalidRequestException; import groovy.util.logging.Slf4j; +import java.io.Serializable; import java.util.*; +import java.util.stream.Collectors; +import java.util.stream.Stream; import javax.servlet.http.HttpServletRequest; import lombok.Data; +import lombok.EqualsAndHashCode; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; @@ -48,7 +52,7 @@ public AliCloudImageController(Cache cacheView) { @RequestMapping(value = "/find", method = RequestMethod.GET) List list(LookupOptions lookupOptions, HttpServletRequest request) { String glob = lookupOptions.getQ(); - if (StringUtils.isAllBlank(glob) && glob.length() < 3) { + if (StringUtils.isAllBlank(glob) || glob.length() < 3) { throw new InvalidRequestException("Lost search condition or length less 3"); } glob = "*" + glob + "*"; @@ -63,8 +67,7 @@ List list(LookupOptions lookupOptions, HttpServletRequest request) { Keys.getNamedImageKey( StringUtils.isAllBlank(lookupOptions.account) ? "*" : lookupOptions.account, glob); Collection nameImageIdentifiers = cacheView.filterIdentifiers(NAMED_IMAGES.ns, nameKey); - Collection nameImages = - cacheView.getAll(NAMED_IMAGES.ns, nameImageIdentifiers, null); + Collection nameImages = cacheView.getAll(NAMED_IMAGES.ns, nameImageIdentifiers); return filter(render(nameImages, images), extractTagFilters(request)); } @@ -81,25 +84,6 @@ private static List filter(List namedImages, Map t return filter; } - /* private static boolean checkInclude(Image image, Map tagFilters) { - boolean flag = false; - List tags = (List) image.getAttributes().get("tags"); - if (tags != null) { - for (Map tag : tags) { - String tagKey = tag.get("tagKey").toString(); - String tagValue = tag.get("tagValue").toString(); - if (StringUtils.isNotEmpty(tagFilters.get(tagKey)) - && tagFilters.get(tagKey).equalsIgnoreCase(tagValue)) { - flag = true; - } else { - flag = false; - break; - } - } - } - return flag; - }*/ - private static boolean checkInclude(Image image, Map tagFilters) { boolean flag = false; List tags = (List) image.getAttributes().get("tags"); @@ -122,17 +106,15 @@ private static boolean checkInclude(Image image, Map tagFilters) } private List render(Collection namedImages, Collection images) { - List list = new ArrayList<>(); - for (CacheData image : images) { - Map attributes = image.getAttributes(); - list.add(new Image(String.valueOf(attributes.get("imageName")), attributes)); - } - - for (CacheData nameImage : namedImages) { - Map attributes = nameImage.getAttributes(); - list.add(new Image(String.valueOf(attributes.get("imageName")), attributes)); - } - return list; + return Stream.of(namedImages.stream(), images.stream()) + .flatMap(s -> s) + .map( + cache -> { + Map attributes = cache.getAttributes(); + return new Image(String.valueOf(attributes.get("imageName")), attributes); + }) + .distinct() + .collect(Collectors.toList()); } private static Map extractTagFilters(HttpServletRequest request) { @@ -150,7 +132,8 @@ private static Map extractTagFilters(HttpServletRequest request) } @Data - public static class Image { + @EqualsAndHashCode + public static class Image implements Serializable { public Image(String imageName, Map attributes) { this.imageName = imageName; this.attributes = attributes; diff --git a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/controllers/AliCloudScalingActivitiesController.java b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/controllers/AliCloudScalingActivitiesController.java index f305f43934b..5fff750e994 100644 --- a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/controllers/AliCloudScalingActivitiesController.java +++ b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/controllers/AliCloudScalingActivitiesController.java @@ -68,8 +68,7 @@ ResponseEntity getScalingActivities( } AliCloudCredentials aliCloudCredentials = (AliCloudCredentials) credentials; IAcsClient client = - clientFactory.createClient( - region, aliCloudCredentials.getAccessKeyId(), aliCloudCredentials.getAccessSecretKey()); + clientFactory.createClient(region, aliCloudCredentials.getCredentialsProvider()); DescribeScalingGroupsRequest describeScalingGroupsRequest = new DescribeScalingGroupsRequest(); describeScalingGroupsRequest.setScalingGroupName(serverGroupName); describeScalingGroupsRequest.setPageSize(50); diff --git a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/model/AliCloudApplication.java b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/model/AliCloudApplication.java new file mode 100644 index 00000000000..ce93033d1f4 --- /dev/null +++ b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/model/AliCloudApplication.java @@ -0,0 +1,54 @@ +/* + * Copyright 2022 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.clouddriver.alicloud.model; + +import com.netflix.spinnaker.clouddriver.model.Application; +import java.io.Serializable; +import java.util.Map; +import java.util.Set; + +public class AliCloudApplication implements Application, Serializable { + private String name; + /** key: account value: cluster name */ + private Map> clusterNames; + + private Map attributes; + + public AliCloudApplication() {} + + public AliCloudApplication( + String name, Map> clusterNames, Map attributes) { + this.name = name; + this.clusterNames = clusterNames; + this.attributes = attributes; + } + + @Override + public String getName() { + return name; + } + + @Override + public Map getAttributes() { + return attributes; + } + + @Override + public Map> getClusterNames() { + return clusterNames; + } +} diff --git a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/model/AliCloudCluster.java b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/model/AliCloudCluster.java index f5f91d0b015..c5e486922fc 100644 --- a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/model/AliCloudCluster.java +++ b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/model/AliCloudCluster.java @@ -16,6 +16,7 @@ package com.netflix.spinnaker.clouddriver.alicloud.model; +import com.netflix.spinnaker.clouddriver.alicloud.AliCloudProvider; import com.netflix.spinnaker.clouddriver.model.Cluster; import java.io.Serializable; import java.util.Set; @@ -23,19 +24,16 @@ public class AliCloudCluster implements Cluster, Serializable { private String name; - private String type; private String accountName; private Set serverGroups; private Set loadBalancers; public AliCloudCluster( String name, - String type, String accountName, Set serverGroups, Set loadBalancers) { this.name = name; - this.type = type; this.accountName = accountName; this.serverGroups = serverGroups; this.loadBalancers = loadBalancers; @@ -48,7 +46,7 @@ public String getName() { @Override public String getType() { - return type; + return AliCloudProvider.ID; } @Override diff --git a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/model/AliCloudInstance.java b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/model/AliCloudInstance.java index ce09d370951..bdd237dae42 100644 --- a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/model/AliCloudInstance.java +++ b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/model/AliCloudInstance.java @@ -16,34 +16,35 @@ package com.netflix.spinnaker.clouddriver.alicloud.model; +import com.netflix.spinnaker.clouddriver.alicloud.AliCloudProvider; import com.netflix.spinnaker.clouddriver.model.HealthState; import com.netflix.spinnaker.clouddriver.model.Instance; +import java.util.HashMap; import java.util.List; import java.util.Map; +import lombok.EqualsAndHashCode; +@EqualsAndHashCode public class AliCloudInstance implements Instance { private String name; private Long launchTime; private String zone; - private String providerType; - private String cloudProvider; + final String cloudProvider = AliCloudProvider.ID; private HealthState healthState; private List> health; + private Map attributes = new HashMap<>(); + public AliCloudInstance( String name, Long launchTime, String zone, - String providerType, - String cloudProvider, HealthState healthState, List> health) { this.name = name; this.launchTime = launchTime; this.zone = zone; - this.providerType = providerType; - this.cloudProvider = cloudProvider; this.healthState = healthState; this.health = health; } @@ -63,11 +64,6 @@ public String getZone() { return zone; } - @Override - public String getProviderType() { - return providerType; - } - @Override public String getCloudProvider() { return cloudProvider; @@ -82,4 +78,12 @@ public HealthState getHealthState() { public List> getHealth() { return health; } + + public Map getAttributes() { + return attributes; + } + + public void setAttributes(Map attributes) { + this.attributes = attributes; + } } diff --git a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/model/AliCloudKeyPair.java b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/model/AliCloudKeyPair.java index 1dae3571fa6..83922343f79 100644 --- a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/model/AliCloudKeyPair.java +++ b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/model/AliCloudKeyPair.java @@ -18,7 +18,11 @@ import com.netflix.spinnaker.clouddriver.alicloud.AliCloudProvider; import com.netflix.spinnaker.clouddriver.model.KeyPair; +import lombok.Data; +import lombok.EqualsAndHashCode; +@Data +@EqualsAndHashCode public class AliCloudKeyPair implements KeyPair { String account; @@ -33,26 +37,4 @@ public AliCloudKeyPair(String account, String region, String keyName, String key this.keyName = keyName; this.keyFingerprint = keyFingerprint; } - - @Override - public String getKeyName() { - return keyName; - } - - @Override - public String getKeyFingerprint() { - return keyFingerprint; - } - - public String getAccount() { - return account; - } - - public String getRegion() { - return region; - } - - public String getCloudProvider() { - return cloudProvider; - } } diff --git a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/model/AliCloudLoadBalancer.java b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/model/AliCloudLoadBalancer.java index d6f41acf653..924c1d7edc9 100644 --- a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/model/AliCloudLoadBalancer.java +++ b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/model/AliCloudLoadBalancer.java @@ -5,7 +5,7 @@ * 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 + * 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, @@ -23,7 +23,11 @@ import java.util.HashSet; import java.util.Map; import java.util.Set; +import lombok.Data; +import lombok.EqualsAndHashCode; +@Data +@EqualsAndHashCode public class AliCloudLoadBalancer implements LoadBalancer { String account; @@ -41,6 +45,7 @@ public class AliCloudLoadBalancer implements LoadBalancer { String vpcId; Set serverGroups = new HashSet<>(); + Set securityGroups; Map labels = new HashMap<>(); @@ -52,50 +57,4 @@ public AliCloudLoadBalancer( this.vpcId = vpcId; this.loadBalancerId = loadBalancerId; } - - @Override - public String getName() { - return name; - } - - @Override - public String getType() { - return type; - } - - @Override - public String getCloudProvider() { - return cloudProvider; - } - - @Override - public String getAccount() { - return account; - } - - @Override - public Set getServerGroups() { - return serverGroups; - } - - @Override - public Map getLabels() { - return labels; - } - - public String getRegion() { - return region; - } - - public String getVpcId() { - return vpcId; - } - - public String getLoadBalancerId() { - return loadBalancerId; - } - - public void setServerGroups(Set serverGroups) { - this.serverGroups = serverGroups; - } } diff --git a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/model/AliCloudSecurityGroup.java b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/model/AliCloudSecurityGroup.java index 4f687684f1f..41694a1a9f3 100644 --- a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/model/AliCloudSecurityGroup.java +++ b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/model/AliCloudSecurityGroup.java @@ -21,10 +21,11 @@ import com.netflix.spinnaker.clouddriver.model.SecurityGroupSummary; import com.netflix.spinnaker.clouddriver.model.securitygroups.Rule; import java.util.Set; +import lombok.EqualsAndHashCode; +@EqualsAndHashCode public class AliCloudSecurityGroup implements SecurityGroup { - String type = AliCloudProvider.ID; String cloudProvider = AliCloudProvider.ID; String id; String name; @@ -52,11 +53,6 @@ public AliCloudSecurityGroup( this.inboundRules = inboundRules; } - @Override - public String getType() { - return type; - } - @Override public String getCloudProvider() { return cloudProvider; @@ -67,16 +63,35 @@ public String getId() { return id; } - @Override - public String getName() { - return name; + public String getVpcId() { + return vpcId; } - @Override - public String getApplication() { + public String getAccount() { + return accountName; + } + + public String getDescription() { return application; } + public String getDetail() { + return getMoniker().getDetail(); + } + + public String getStack() { + return getMoniker().getApp(); + } + + public String getCredentials() { + return accountName; + } + + @Override + public String getName() { + return name; + } + @Override public String getAccountName() { return accountName; diff --git a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/model/AliCloudServerGroup.java b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/model/AliCloudServerGroup.java index d0b2fc53690..afa37647adf 100644 --- a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/model/AliCloudServerGroup.java +++ b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/model/AliCloudServerGroup.java @@ -23,8 +23,10 @@ import java.util.Map; import java.util.Set; import lombok.Data; +import lombok.EqualsAndHashCode; @Data +@EqualsAndHashCode public class AliCloudServerGroup implements ServerGroup, Serializable { private String name; @@ -133,24 +135,21 @@ public ImageSummary getImageSummary() { @Override public ImagesSummary getImagesSummary() { - return new ImagesSummary() { - @Override - public List getSummaries() { - List list = new ArrayList<>(); - InnSum innSum = new InnSum(image, buildInfo, name); - list.add(innSum); - return list; - } + return () -> { + List list = new ArrayList<>(); + InnerSummary summary = new InnerSummary(image, buildInfo, name); + list.add(summary); + return list; }; } - public class InnSum implements ImageSummary { + public class InnerSummary implements ServerGroup.ImageSummary { private Map i; private Map bi; private String serverGroupName; - public InnSum(Map i, Map bi, String serverGroupName) { + public InnerSummary(Map i, Map bi, String serverGroupName) { this.i = i; this.bi = bi; this.serverGroupName = serverGroupName; diff --git a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/model/AliCloudSubnet.java b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/model/AliCloudSubnet.java index 017f2e0171c..b28da99ff11 100644 --- a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/model/AliCloudSubnet.java +++ b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/model/AliCloudSubnet.java @@ -38,6 +38,8 @@ public class AliCloudSubnet implements Subnet { private String type; + private String vpcName; + public AliCloudSubnet() {} public AliCloudSubnet( @@ -48,7 +50,8 @@ public AliCloudSubnet( String vSwitchName, String vpcId, String zoneId, - String type) { + String type, + String vpcName) { this.account = account; this.region = region; this.status = status; @@ -57,6 +60,7 @@ public AliCloudSubnet( this.vpcId = vpcId; this.zoneId = zoneId; this.type = type; + this.vpcName = vpcName; } @Override diff --git a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/model/alienum/LifecycleState.java b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/model/alienum/LifecycleState.java new file mode 100644 index 00000000000..47603778d47 --- /dev/null +++ b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/model/alienum/LifecycleState.java @@ -0,0 +1,23 @@ +/* + * Copyright 2022 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.clouddriver.alicloud.model.alienum; + +/** https://www.alibabacloud.com/help/en/auto-scaling/latest/describescalingconfigurations */ +public enum LifecycleState { + Active, + Inacitve +} diff --git a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/model/alienum/ListenerType.java b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/model/alienum/ListenerType.java index d61d3f85aa5..4ee8b1b7ab8 100644 --- a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/model/alienum/ListenerType.java +++ b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/model/alienum/ListenerType.java @@ -16,9 +16,22 @@ package com.netflix.spinnaker.clouddriver.alicloud.model.alienum; +import com.fasterxml.jackson.annotation.JsonCreator; + public enum ListenerType { HTTP, HTTPS, TCP, - UDP + UDP; + + @JsonCreator + public static ListenerType forName(String name) { + for (ListenerType l : values()) { + if (l.name().equals(name)) { + return l; + } + } + + return null; + } } diff --git a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/model/alienum/ScalingInstanceHealthStatus.java b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/model/alienum/ScalingInstanceHealthStatus.java new file mode 100644 index 00000000000..3dc7da7e419 --- /dev/null +++ b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/model/alienum/ScalingInstanceHealthStatus.java @@ -0,0 +1,35 @@ +/* + * Copyright 2022 Netflix, 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.netflix.spinnaker.clouddriver.alicloud.model.alienum; + +import com.netflix.spinnaker.clouddriver.model.HealthState; + +public enum ScalingInstanceHealthStatus { + Healthy, + Unhealthy, + Unknown; + + public static ScalingInstanceHealthStatus forState(HealthState state) { + if (HealthState.Unknown == state) { + return Unknown; + } + if (HealthState.Up == state) { + return Healthy; + } + return Unhealthy; + } +} diff --git a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/provider/AliProvider.java b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/provider/AliProvider.java index b43af1ec508..9eda7a6348b 100644 --- a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/provider/AliProvider.java +++ b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/provider/AliProvider.java @@ -5,7 +5,7 @@ * 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 + * 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, @@ -16,25 +16,31 @@ package com.netflix.spinnaker.clouddriver.alicloud.provider; import static com.netflix.spinnaker.clouddriver.alicloud.cache.Keys.Namespace.SECURITY_GROUPS; -import static com.netflix.spinnaker.clouddriver.core.provider.agent.Namespace.*; +import static com.netflix.spinnaker.clouddriver.core.provider.agent.Namespace.CLUSTERS; +import static com.netflix.spinnaker.clouddriver.core.provider.agent.Namespace.INSTANCES; +import static com.netflix.spinnaker.clouddriver.core.provider.agent.Namespace.LOAD_BALANCERS; +import static com.netflix.spinnaker.clouddriver.core.provider.agent.Namespace.SERVER_GROUPS; +import static com.netflix.spinnaker.clouddriver.core.provider.agent.Namespace.TARGET_GROUPS; import com.netflix.spinnaker.cats.agent.Agent; import com.netflix.spinnaker.cats.agent.AgentSchedulerAware; import com.netflix.spinnaker.clouddriver.alicloud.cache.Keys; import com.netflix.spinnaker.clouddriver.cache.SearchableProvider; -import com.netflix.spinnaker.clouddriver.security.AccountCredentialsRepository; -import java.util.*; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; @ConditionalOnProperty("alicloud.enabled") public class AliProvider extends AgentSchedulerAware implements SearchableProvider { public static final String PROVIDER_NAME = AliProvider.class.getName(); - private final AccountCredentialsRepository accountCredentialsRepository; private final Collection agents; - AliProvider(AccountCredentialsRepository accountCredentialsRepository, Collection agents) { - this.accountCredentialsRepository = accountCredentialsRepository; + AliProvider(Collection agents) { this.agents = agents; } @@ -49,7 +55,7 @@ public Collection getAgents() { } final Set defaultCaches = - new HashSet() { + new HashSet<>() { { add(LOAD_BALANCERS.ns); add(CLUSTERS.ns); @@ -61,7 +67,7 @@ public Collection getAgents() { }; final Map urlMappingTemplates = - new HashMap() { + new HashMap<>() { { put( SERVER_GROUPS.ns, @@ -72,7 +78,7 @@ public Collection getAgents() { } }; - final Map searchResultHydrators = + final Map searchResultHydrators = Collections.emptyMap(); @Override diff --git a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/provider/AliProviderConfig.java b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/provider/AliProviderConfig.java index afd6358668f..8df94a078c4 100644 --- a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/provider/AliProviderConfig.java +++ b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/provider/AliProviderConfig.java @@ -15,6 +15,7 @@ */ package com.netflix.spinnaker.clouddriver.alicloud.provider; +import com.aliyuncs.IAcsClient; import com.fasterxml.jackson.databind.ObjectMapper; import com.netflix.spectator.api.Registry; import com.netflix.spinnaker.cats.agent.Agent; @@ -52,10 +53,7 @@ public AliProvider aliProvider( AliCloudProvider aliCloudProvider, ApplicationContext ctx, ClientFactory clientFactory) { - AliProvider provider = - new AliProvider( - accountCredentialsRepository, - Collections.newSetFromMap(new ConcurrentHashMap())); + AliProvider provider = new AliProvider(Collections.newSetFromMap(new ConcurrentHashMap<>())); synchronizeAliProvider( provider, accountCredentialsRepository, @@ -93,6 +91,9 @@ public AliProviderSynchronizer synchronizeAliProvider( for (String region : credentials.getRegions()) { if (!scheduledAccounts.contains(credentials.getName())) { + IAcsClient client = + clientFactory.createClient(region, credentials.getCredentialsProvider()); + newAgents.add( new AliCloudLoadBalancerCachingAgent( aliProvider, @@ -103,23 +104,23 @@ public AliProviderSynchronizer synchronizeAliProvider( objectMapper, registry, credentials, - clientFactory.createClient( - region, credentials.getAccessKeyId(), credentials.getAccessSecretKey()))); + client)); newAgents.add( - new AliCloudLoadBalancerInstanceStateCachingAgent( - ctx, - credentials, - region, - objectMapper, - clientFactory.createClient( - region, credentials.getAccessKeyId(), credentials.getAccessSecretKey()))); + new AliCloudSubnetCachingAgent(credentials, region, objectMapper, client)); + newAgents.add(new AliCloudImageCachingAgent(credentials, region, objectMapper, client)); newAgents.add( - new AliCloudSecurityGroupCachingAgent( - credentials, - region, - objectMapper, - clientFactory.createClient( - region, credentials.getAccessKeyId(), credentials.getAccessSecretKey()))); + new AliCloudInstanceTypeCachingAgent(credentials, region, objectMapper, client)); + newAgents.add( + new AliCloudSecurityGroupCachingAgent(credentials, region, objectMapper, client)); + newAgents.add( + new AliCloudKeyPairCachingAgent(credentials, region, objectMapper, client)); + newAgents.add( + new AliCloudClusterCachingAgent(credentials, region, objectMapper, client)); + newAgents.add( + new AliCloudInstanceCachingAgent(credentials, region, objectMapper, client)); + newAgents.add( + new AliCloudLoadBalancerInstanceStateCachingAgent( + ctx, credentials, region, objectMapper, client)); } } } diff --git a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/provider/agent/AliCloudClusterCachingAgent.java b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/provider/agent/AliCloudClusterCachingAgent.java new file mode 100644 index 00000000000..38ef7411a54 --- /dev/null +++ b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/provider/agent/AliCloudClusterCachingAgent.java @@ -0,0 +1,488 @@ +/* + * Copyright 2022 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.clouddriver.alicloud.provider.agent; + +import static com.netflix.spinnaker.cats.agent.AgentDataType.Authority.AUTHORITATIVE; +import static com.netflix.spinnaker.cats.agent.AgentDataType.Authority.INFORMATIVE; +import static com.netflix.spinnaker.clouddriver.core.provider.agent.Namespace.APPLICATIONS; +import static com.netflix.spinnaker.clouddriver.core.provider.agent.Namespace.CLUSTERS; +import static com.netflix.spinnaker.clouddriver.core.provider.agent.Namespace.INSTANCES; +import static com.netflix.spinnaker.clouddriver.core.provider.agent.Namespace.LAUNCH_CONFIGS; +import static com.netflix.spinnaker.clouddriver.core.provider.agent.Namespace.LOAD_BALANCERS; +import static com.netflix.spinnaker.clouddriver.core.provider.agent.Namespace.SERVER_GROUPS; + +import com.aliyuncs.IAcsClient; +import com.aliyuncs.ecs.model.v20140526.DescribeInstancesRequest; +import com.aliyuncs.ecs.model.v20140526.DescribeInstancesResponse; +import com.aliyuncs.ecs.model.v20140526.DescribeInstancesResponse.Instance; +import com.aliyuncs.ecs.model.v20140526.DescribeSecurityGroupsRequest; +import com.aliyuncs.ecs.model.v20140526.DescribeSecurityGroupsResponse; +import com.aliyuncs.ecs.model.v20140526.DescribeSecurityGroupsResponse.SecurityGroup; +import com.aliyuncs.ess.model.v20140828.DescribeScalingConfigurationsRequest; +import com.aliyuncs.ess.model.v20140828.DescribeScalingConfigurationsResponse; +import com.aliyuncs.ess.model.v20140828.DescribeScalingConfigurationsResponse.ScalingConfiguration; +import com.aliyuncs.ess.model.v20140828.DescribeScalingGroupsRequest; +import com.aliyuncs.ess.model.v20140828.DescribeScalingGroupsResponse; +import com.aliyuncs.ess.model.v20140828.DescribeScalingGroupsResponse.ScalingGroup; +import com.aliyuncs.ess.model.v20140828.DescribeScalingGroupsResponse.ScalingGroup.VServerGroup; +import com.aliyuncs.ess.model.v20140828.DescribeScalingInstancesRequest; +import com.aliyuncs.ess.model.v20140828.DescribeScalingInstancesResponse; +import com.aliyuncs.ess.model.v20140828.DescribeScalingInstancesResponse.ScalingInstance; +import com.aliyuncs.exceptions.ClientException; +import com.aliyuncs.slb.model.v20140515.DescribeLoadBalancerAttributeRequest; +import com.aliyuncs.slb.model.v20140515.DescribeLoadBalancerAttributeResponse; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.netflix.frigga.Names; +import com.netflix.spinnaker.cats.agent.AccountAware; +import com.netflix.spinnaker.cats.agent.AgentDataType; +import com.netflix.spinnaker.cats.agent.CacheResult; +import com.netflix.spinnaker.cats.agent.CachingAgent; +import com.netflix.spinnaker.cats.agent.DefaultCacheResult; +import com.netflix.spinnaker.cats.cache.CacheData; +import com.netflix.spinnaker.cats.cache.DefaultCacheData; +import com.netflix.spinnaker.cats.provider.ProviderCache; +import com.netflix.spinnaker.clouddriver.alicloud.AliCloudProvider; +import com.netflix.spinnaker.clouddriver.alicloud.cache.Keys; +import com.netflix.spinnaker.clouddriver.alicloud.common.CacheDataHelper; +import com.netflix.spinnaker.clouddriver.alicloud.common.Sets; +import com.netflix.spinnaker.clouddriver.alicloud.provider.AliProvider; +import com.netflix.spinnaker.clouddriver.alicloud.security.AliCloudCredentials; +import com.netflix.spinnaker.clouddriver.cache.OnDemandAgent; +import com.netflix.spinnaker.clouddriver.cache.OnDemandMetricsSupport; +import com.netflix.spinnaker.clouddriver.cache.OnDemandType; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; +import java.util.Set; +import java.util.stream.Collectors; +import org.jetbrains.annotations.NotNull; +import org.springframework.util.CollectionUtils; + +public class AliCloudClusterCachingAgent implements CachingAgent, AccountAware, OnDemandAgent { + + private AliCloudCredentials account; + private String region; + ObjectMapper objectMapper; + IAcsClient client; + + public AliCloudClusterCachingAgent( + AliCloudCredentials account, String region, ObjectMapper objectMapper, IAcsClient client) { + this.account = account; + this.region = region; + this.objectMapper = objectMapper; + this.client = client; + } + + static final Collection types = + Collections.unmodifiableCollection( + new ArrayList<>() { + { + add(AUTHORITATIVE.forType(CLUSTERS.ns)); + add(AUTHORITATIVE.forType(SERVER_GROUPS.ns)); + add(AUTHORITATIVE.forType(APPLICATIONS.ns)); + add(INFORMATIVE.forType(LAUNCH_CONFIGS.ns)); + } + }); + + @Override + public CacheResult loadData(ProviderCache providerCache) { + CacheResult result = new DefaultCacheResult(new HashMap<>(16)); + try { + List sgData = loadSgDatas(); + + result = buildCacheResult(sgData); + } catch (Exception e) { + e.printStackTrace(); + } + + return result; + } + + @NotNull + private List loadSgDatas() throws ClientException { + List scalingGroups = this.findAllScalingGroups(); + List sgData = new ArrayList<>(); + for (ScalingGroup sg : scalingGroups) { + String scalingGroupId = sg.getScalingGroupId(); + String activeScalingConfigurationId = sg.getActiveScalingConfigurationId(); + + DescribeScalingConfigurationsResponse scalingConfigurationsResponse = + findScalingConfigurations(activeScalingConfigurationId, scalingGroupId); + List securityGroups = + findSecurityGroups(scalingConfigurationsResponse.getScalingConfigurations()); + List loadBalancerAttributes = + findLoadBalancerAttributes(sg); + List scalingInstances = + findAllScalingInstances(scalingGroupId, activeScalingConfigurationId); + + sgData.add( + new ClusterCachingBuilder( + sg, + account.getName(), + region, + scalingConfigurationsResponse, + loadBalancerAttributes, + scalingInstances, + securityGroups)); + } + return sgData; + } + + private List findAllScalingGroups() throws ClientException { + int pageNumber = 1; + int pageSize = 50; + List scalingGroups = new ArrayList<>(); + + DescribeScalingGroupsRequest describeScalingGroupsRequest = new DescribeScalingGroupsRequest(); + DescribeScalingGroupsResponse describeScalingGroupsResponse; + while (true) { + describeScalingGroupsRequest.setPageSize(pageSize); + describeScalingGroupsRequest.setPageNumber(pageNumber); + describeScalingGroupsResponse = client.getAcsResponse(describeScalingGroupsRequest); + if (CollectionUtils.isEmpty(describeScalingGroupsResponse.getScalingGroups())) { + break; + } else { + pageNumber = pageNumber + 1; + scalingGroups.addAll(describeScalingGroupsResponse.getScalingGroups()); + } + if (describeScalingGroupsResponse.getScalingGroups().size() < pageSize) { + break; + } + } + return scalingGroups; + } + + private DescribeScalingConfigurationsResponse findScalingConfigurations( + String activeScalingConfigurationId, String scalingGroupId) throws ClientException { + DescribeScalingConfigurationsRequest scalingConfigurationsRequest = + new DescribeScalingConfigurationsRequest(); + scalingConfigurationsRequest.setScalingGroupId(scalingGroupId); + scalingConfigurationsRequest.setScalingConfigurationId1(activeScalingConfigurationId); + return client.getAcsResponse(scalingConfigurationsRequest); + } + + private List findSecurityGroups(List scalingConfigurations) + throws ClientException { + if (scalingConfigurations.size() > 0) { + ScalingConfiguration scalingConfiguration = scalingConfigurations.get(0); + String securityGroupId = scalingConfiguration.getSecurityGroupId(); + DescribeSecurityGroupsRequest securityGroupsRequest = new DescribeSecurityGroupsRequest(); + securityGroupsRequest.setSecurityGroupId(securityGroupId); + DescribeSecurityGroupsResponse securityGroupsResponse = + client.getAcsResponse(securityGroupsRequest); + return securityGroupsResponse.getSecurityGroups(); + } + return Collections.emptyList(); + } + + private List findLoadBalancerAttributes(ScalingGroup sg) { + List loadBalancerIds = sg.getLoadBalancerIds(); + if (sg.getVServerGroups() != null) { + loadBalancerIds.addAll( + sg.getVServerGroups().stream() + .map(VServerGroup::getLoadBalancerId) + .filter(loadBalancerId -> !loadBalancerIds.contains(loadBalancerId)) + .collect(Collectors.toList())); + } + + return loadBalancerIds.stream() + .map( + loadBalancerId -> { + try { + DescribeLoadBalancerAttributeRequest describeLoadBalancerAttributeRequest = + new DescribeLoadBalancerAttributeRequest(); + describeLoadBalancerAttributeRequest.setLoadBalancerId(loadBalancerId); + return client.getAcsResponse(describeLoadBalancerAttributeRequest); + } catch (ClientException e) { + String message = e.getMessage(); + if (message.contains("InvalidLoadBalancerId.NotFound")) { + logger.info(loadBalancerId + " -> NotFound"); + } else { + throw new IllegalStateException(e.getMessage()); + } + } + return null; + }) + .filter(Objects::nonNull) + .collect(Collectors.toList()); + } + + private List findAllScalingInstances( + String scalingGroupId, String activeScalingConfigurationId) throws ClientException { + DescribeScalingInstancesRequest scalingInstancesRequest = new DescribeScalingInstancesRequest(); + scalingInstancesRequest.setScalingGroupId(scalingGroupId); + scalingInstancesRequest.setScalingConfigurationId(activeScalingConfigurationId); + int pageNumber = 1; + int pageSize = 50; + DescribeScalingInstancesResponse scalingInstancesResponse; + List scalingInstances = new ArrayList<>(); + while (true) { + scalingInstancesRequest.setPageNumber(pageNumber); + scalingInstancesRequest.setPageSize(pageSize); + scalingInstancesResponse = client.getAcsResponse(scalingInstancesRequest); + if (CollectionUtils.isEmpty(scalingInstancesResponse.getScalingInstances())) { + break; + } else { + pageNumber = pageNumber + 1; + for (ScalingInstance scalingInstance : scalingInstancesResponse.getScalingInstances()) { + if (scalingInstance.getInstanceId() != null) { + String instanceIds = "[\"" + scalingInstance.getInstanceId() + "\"]"; + DescribeInstancesRequest request = new DescribeInstancesRequest(); + request.setInstanceIds(instanceIds); + DescribeInstancesResponse acsResponse = client.getAcsResponse(request); + if (acsResponse.getInstances() != null && acsResponse.getInstances().size() > 0) { + Instance instance = acsResponse.getInstances().get(0); + String zoneId = instance.getZoneId(); + scalingInstance.setCreationType(zoneId); + } + } + scalingInstances.add(scalingInstance); + } + if (scalingInstancesResponse.getScalingInstances().size() < pageSize) { + break; + } + } + } + return scalingInstances; + } + + private CacheResult buildCacheResult(List sgData) { + + Map> resultMap = new HashMap<>(16); + resultMap.put( + APPLICATIONS.ns, + sgData.stream() + .map(ClusterCachingBuilder::buildApplicationCacheData) + .collect(Collectors.toMap(CacheData::getId, d -> d, CacheDataHelper::merge)) + .values()); + resultMap.put( + CLUSTERS.ns, + sgData.stream() + .map(ClusterCachingBuilder::buildClusterCacheData) + .collect(Collectors.toMap(CacheData::getId, d -> d, CacheDataHelper::merge)) + .values()); + resultMap.put( + SERVER_GROUPS.ns, + sgData.stream() + .map(s -> s.buildServerGroupCacheData(objectMapper)) + .collect(Collectors.toMap(CacheData::getId, d -> d, CacheDataHelper::merge)) + .values()); + + resultMap.put( + LAUNCH_CONFIGS.ns, + sgData.stream() + .map(ClusterCachingBuilder::buildLaunchConfigCacheData) + .collect(Collectors.toMap(CacheData::getId, d -> d, CacheDataHelper::merge)) + .values()); + + return new DefaultCacheResult(resultMap); + } + + public static class ClusterCachingBuilder { + final ScalingGroup scalingGroup; + final DescribeScalingConfigurationsResponse scalingConfigurationsResponse; + final List scalingInstances; + final List loadBalancerAttributes; + final Names name; + final String applicationKey; + final String clusterKey; + final String serverGroupKey; + final String launchConfigKey; + final Set loadBalancerKeys = new HashSet<>(); + final Set instanceIds = new HashSet<>(); + final List securityGroups; + final String accountName; + final String region; + + public ClusterCachingBuilder( + ScalingGroup scalingGroup, + String account, + String region, + DescribeScalingConfigurationsResponse scalingConfigurationsResponse, + List loadBalancerAttributes, + List scalingInstances, + List securityGroups) { + this.scalingGroup = scalingGroup; + this.scalingConfigurationsResponse = scalingConfigurationsResponse; + this.scalingInstances = scalingInstances; + this.loadBalancerAttributes = loadBalancerAttributes; + this.region = region; + this.accountName = account; + this.securityGroups = securityGroups; + + name = Names.parseName(scalingGroup.getScalingGroupName()); + applicationKey = Keys.getApplicationKey(name.getApp()); + clusterKey = Keys.getClusterKey(name.getCluster(), name.getApp(), account); + serverGroupKey = Keys.getServerGroupKey(scalingGroup.getScalingGroupName(), account, region); + launchConfigKey = + Keys.getLaunchConfigKey(scalingGroup.getScalingGroupName(), account, region); + Optional.ofNullable(loadBalancerAttributes) + .ifPresent( + l -> { + loadBalancerKeys.addAll( + l.stream() + .map( + loadBalancerAttribute -> + (Keys.getLoadBalancerKey( + loadBalancerAttribute.getLoadBalancerName(), + account, + region, + loadBalancerAttribute.getVpcId()))) + .collect(Collectors.toSet())); + }); + Optional.ofNullable(scalingInstances) + .ifPresent( + instances -> { + instanceIds.addAll( + instances.stream() + .map( + scalingInstance -> + Keys.getInstanceKey( + scalingInstance.getInstanceId(), account, region)) + .collect(Collectors.toSet())); + }); + } + + public CacheData buildApplicationCacheData() { + Map attributes = new HashMap<>(); + attributes.put("name", this.name.getApp()); + + Map> relationships = new HashMap<>(16); + relationships.put(CLUSTERS.ns, Sets.ofModifiable(this.clusterKey)); + relationships.put(SERVER_GROUPS.ns, Sets.ofModifiable(this.serverGroupKey)); + relationships.put(LOAD_BALANCERS.ns, this.loadBalancerKeys); + + return new DefaultCacheData(applicationKey, attributes, relationships); + } + + public CacheData buildClusterCacheData() { + Map attributes = new HashMap<>(); + attributes.put("name", this.name.getCluster()); + attributes.put("application", this.name.getApp()); + attributes.put("accountName", this.accountName); + + Map> relationships = new HashMap<>(); + relationships.put(APPLICATIONS.ns, Sets.ofModifiable(this.applicationKey)); + relationships.put(SERVER_GROUPS.ns, Sets.ofModifiable(this.serverGroupKey)); + relationships.put(LOAD_BALANCERS.ns, this.loadBalancerKeys); + + return new DefaultCacheData(clusterKey, attributes, relationships); + } + + public CacheData buildServerGroupCacheData(ObjectMapper objectMapper) { + Map attributes = new HashMap<>(16); + attributes.put("application", this.name.getApp()); + attributes.put("scalingGroup", this.scalingGroup); + attributes.put("region", this.region); + attributes.put("name", this.scalingGroup.getScalingGroupName()); + if (this.scalingConfigurationsResponse.getScalingConfigurations().size() > 0) { + attributes.put( + "launchConfigName", + this.scalingConfigurationsResponse + .getScalingConfigurations() + .get(0) + .getScalingConfigurationName()); + ScalingConfiguration scalingConfiguration = + this.scalingConfigurationsResponse.getScalingConfigurations().get(0); + Map map = objectMapper.convertValue(scalingConfiguration, Map.class); + map.put( + "securityGroupName", + securityGroups.stream() + .map(SecurityGroup::getSecurityGroupName) + .collect(Collectors.joining(","))); + map.put( + "securityGroupIds", + securityGroups.stream() + .map(SecurityGroup::getSecurityGroupId) + .collect(Collectors.toSet())); + attributes.put("scalingConfiguration", map); + } else { + attributes.put("scalingConfiguration", new ScalingConfiguration()); + } + attributes.put("instances", this.scalingInstances); + attributes.put("loadBalancers", this.loadBalancerAttributes); + attributes.put("provider", AliCloudProvider.ID); + attributes.put("account", this.accountName); + attributes.put("regionId", this.region); + + Map> relationships = new HashMap<>(); + relationships.put(APPLICATIONS.ns, Sets.ofModifiable(this.applicationKey)); + relationships.put(CLUSTERS.ns, Sets.ofModifiable(this.clusterKey)); + relationships.put(LOAD_BALANCERS.ns, this.loadBalancerKeys); + relationships.put(LAUNCH_CONFIGS.ns, Sets.ofModifiable(this.launchConfigKey)); + relationships.put(INSTANCES.ns, this.instanceIds); + return new DefaultCacheData(serverGroupKey, attributes, relationships); + } + + public CacheData buildLaunchConfigCacheData() { + Map attributes = new HashMap<>(16); + Map> relationships = new HashMap<>(16); + relationships.put(SERVER_GROUPS.ns, Sets.ofModifiable(this.serverGroupKey)); + return new DefaultCacheData(launchConfigKey, attributes, relationships); + } + } + + @Override + public OnDemandResult handle(ProviderCache providerCache, Map data) { + return null; + } + + @Override + public String getAgentType() { + return account.getName() + "/" + region + "/" + this.getClass().getSimpleName(); + } + + @Override + public String getProviderName() { + return AliProvider.PROVIDER_NAME; + } + + @Override + public String getAccountName() { + return account.getName(); + } + + @Override + public String getOnDemandAgentType() { + return this.getAgentType() + "-OnDemand"; + } + + @Override + public OnDemandMetricsSupport getMetricsSupport() { + return null; + } + + @Override + public boolean handles(OnDemandType type, String cloudProvider) { + return false; + } + + @Override + public Collection> pendingOnDemandRequests(ProviderCache providerCache) { + return null; + } + + @Override + public Collection getProvidedDataTypes() { + return types; + } +} diff --git a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/provider/agent/AliCloudImageCachingAgent.java b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/provider/agent/AliCloudImageCachingAgent.java new file mode 100644 index 00000000000..84098d2055d --- /dev/null +++ b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/provider/agent/AliCloudImageCachingAgent.java @@ -0,0 +1,158 @@ +/* + * Copyright 2022 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.clouddriver.alicloud.provider.agent; + +import static com.netflix.spinnaker.cats.agent.AgentDataType.Authority.AUTHORITATIVE; +import static com.netflix.spinnaker.clouddriver.core.provider.agent.Namespace.IMAGES; +import static com.netflix.spinnaker.clouddriver.core.provider.agent.Namespace.NAMED_IMAGES; + +import com.aliyuncs.IAcsClient; +import com.aliyuncs.ecs.model.v20140526.DescribeImagesRequest; +import com.aliyuncs.ecs.model.v20140526.DescribeImagesResponse; +import com.aliyuncs.ecs.model.v20140526.DescribeImagesResponse.Image; +import com.aliyuncs.exceptions.ClientException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.netflix.spinnaker.cats.agent.AccountAware; +import com.netflix.spinnaker.cats.agent.AgentDataType; +import com.netflix.spinnaker.cats.agent.CacheResult; +import com.netflix.spinnaker.cats.agent.CachingAgent; +import com.netflix.spinnaker.cats.agent.DefaultCacheResult; +import com.netflix.spinnaker.cats.cache.CacheData; +import com.netflix.spinnaker.cats.cache.DefaultCacheData; +import com.netflix.spinnaker.cats.provider.ProviderCache; +import com.netflix.spinnaker.clouddriver.alicloud.cache.Keys; +import com.netflix.spinnaker.clouddriver.alicloud.provider.AliProvider; +import com.netflix.spinnaker.clouddriver.alicloud.security.AliCloudCredentials; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; +import org.springframework.util.CollectionUtils; + +public class AliCloudImageCachingAgent implements CachingAgent, AccountAware { + + AliCloudCredentials account; + String region; + ObjectMapper objectMapper; + IAcsClient client; + + public AliCloudImageCachingAgent( + AliCloudCredentials account, String region, ObjectMapper objectMapper, IAcsClient client) { + this.account = account; + this.region = region; + this.objectMapper = objectMapper; + this.client = client; + } + + static final Collection types = + Collections.unmodifiableCollection( + new ArrayList<>() { + { + add(AUTHORITATIVE.forType(IMAGES.ns)); + add(AUTHORITATIVE.forType(NAMED_IMAGES.ns)); + } + }); + + @Override + public CacheResult loadData(ProviderCache providerCache) { + + List images = findAllImages(); + + List listMaps = + images.stream() + .map(image -> objectMapper.convertValue(image, Map.class)) + .collect(Collectors.toList()); + + List imageDatas = + listMaps.stream() + .map( + map -> + new DefaultCacheData( + Keys.getImageKey( + String.valueOf(map.get("imageId")), account.getName(), region), + map, + new HashMap<>(16))) + .collect(Collectors.toList()); + + List nameImageDatas = + listMaps.stream() + .map( + map -> + new DefaultCacheData( + Keys.getNamedImageKey( + account.getName(), String.valueOf(map.get("imageName"))), + map, + new HashMap<>(16))) + .collect(Collectors.toList()); + + Map> resultMap = new HashMap<>(16); + resultMap.put(IMAGES.ns, imageDatas); + resultMap.put(NAMED_IMAGES.ns, nameImageDatas); + + return new DefaultCacheResult(resultMap); + } + + private List findAllImages() { + int imagePageNumber = 1; + int imagePageSize = 50; + + DescribeImagesRequest imagesRequest = new DescribeImagesRequest(); + imagesRequest.setPageSize(imagePageSize); + List images = new ArrayList<>(128); + try { + while (true) { + imagesRequest.setPageNumber(imagePageNumber); + DescribeImagesResponse describeImagesResponse = client.getAcsResponse(imagesRequest); + if (CollectionUtils.isEmpty(describeImagesResponse.getImages())) { + break; + } else { + imagePageNumber = imagePageNumber + 1; + images.addAll(describeImagesResponse.getImages()); + + if (describeImagesResponse.getImages().size() < imagePageSize) { + break; + } + } + } + } catch (ClientException e) { + e.printStackTrace(); + } + return images; + } + + @Override + public Collection getProvidedDataTypes() { + return types; + } + + @Override + public String getAgentType() { + return account.getName() + "/" + region + "/" + this.getClass().getSimpleName(); + } + + @Override + public String getProviderName() { + return AliProvider.PROVIDER_NAME; + } + + @Override + public String getAccountName() { + return account.getName(); + } +} diff --git a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/provider/agent/AliCloudInstanceCachingAgent.java b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/provider/agent/AliCloudInstanceCachingAgent.java new file mode 100644 index 00000000000..9d1fee526a6 --- /dev/null +++ b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/provider/agent/AliCloudInstanceCachingAgent.java @@ -0,0 +1,133 @@ +/* + * Copyright 2022 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.clouddriver.alicloud.provider.agent; + +import static com.netflix.spinnaker.cats.agent.AgentDataType.Authority.AUTHORITATIVE; +import static com.netflix.spinnaker.clouddriver.core.provider.agent.Namespace.INSTANCES; + +import com.aliyuncs.IAcsClient; +import com.aliyuncs.ecs.model.v20140526.DescribeInstancesRequest; +import com.aliyuncs.ecs.model.v20140526.DescribeInstancesResponse; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.netflix.spinnaker.cats.agent.AgentDataType; +import com.netflix.spinnaker.cats.agent.CacheResult; +import com.netflix.spinnaker.cats.agent.CachingAgent; +import com.netflix.spinnaker.cats.agent.DefaultCacheResult; +import com.netflix.spinnaker.cats.cache.CacheData; +import com.netflix.spinnaker.cats.cache.DefaultCacheData; +import com.netflix.spinnaker.cats.provider.ProviderCache; +import com.netflix.spinnaker.clouddriver.alicloud.cache.Keys; +import com.netflix.spinnaker.clouddriver.alicloud.provider.AliProvider; +import com.netflix.spinnaker.clouddriver.alicloud.security.AliCloudCredentials; +import java.util.*; +import java.util.stream.Collectors; +import org.springframework.util.CollectionUtils; + +public class AliCloudInstanceCachingAgent implements CachingAgent { + + AliCloudCredentials account; + String region; + ObjectMapper objectMapper; + IAcsClient client; + + public AliCloudInstanceCachingAgent( + AliCloudCredentials account, String region, ObjectMapper objectMapper, IAcsClient client) { + this.account = account; + this.region = region; + this.objectMapper = objectMapper; + this.client = client; + } + + static final Collection types = + Collections.unmodifiableCollection( + new ArrayList<>() { + { + add(AUTHORITATIVE.forType(INSTANCES.ns)); + } + }); + + @Override + public CacheResult loadData(ProviderCache providerCache) { + + List instances = findAllInstances(); + + List instanceDatas = + instances.stream() + .map( + instance -> { + String bizRegionId = instance.getBizRegionId(); + Map attributes = objectMapper.convertValue(instance, Map.class); + attributes.put("account", account.getName()); + attributes.put("regionId", bizRegionId); + attributes.put("zoneId", instance.getZoneId()); + + return (CacheData) + new DefaultCacheData( + Keys.getInstanceKey( + instance.getInstanceId(), account.getName(), bizRegionId), + attributes, + new HashMap<>(16)); + }) + .collect(Collectors.toList()); + + Map> resultMap = new HashMap<>(16); + resultMap.put(INSTANCES.ns, instanceDatas); + return new DefaultCacheResult(resultMap); + } + + private List findAllInstances() { + + List instances = new ArrayList<>(32); + + DescribeInstancesRequest describeInstancesRequest = new DescribeInstancesRequest(); + describeInstancesRequest.setSysRegionId(region); + int pageNumber = 1; + int pageSize = 10; + + DescribeInstancesResponse describeInstancesResponse; + try { + while (true) { + describeInstancesRequest.setPageNumber(pageNumber); + describeInstancesRequest.setPageSize(pageSize); + describeInstancesResponse = client.getAcsResponse(describeInstancesRequest); + if (CollectionUtils.isEmpty(describeInstancesResponse.getInstances())) { + break; + } else { + pageNumber = pageNumber + 1; + instances.addAll(describeInstancesResponse.getInstances()); + } + } + } catch (Exception e) { + e.printStackTrace(); + } + return instances; + } + + @Override + public Collection getProvidedDataTypes() { + return types; + } + + @Override + public String getAgentType() { + return account.getName() + "/" + region + "/" + this.getClass().getSimpleName(); + } + + @Override + public String getProviderName() { + return AliProvider.PROVIDER_NAME; + } +} diff --git a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/provider/agent/AliCloudInstanceTypeCachingAgent.java b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/provider/agent/AliCloudInstanceTypeCachingAgent.java new file mode 100644 index 00000000000..75f7667968d --- /dev/null +++ b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/provider/agent/AliCloudInstanceTypeCachingAgent.java @@ -0,0 +1,155 @@ +/* + * Copyright 2022 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.clouddriver.alicloud.provider.agent; + +import static com.netflix.spinnaker.cats.agent.AgentDataType.Authority.AUTHORITATIVE; + +import com.aliyuncs.IAcsClient; +import com.aliyuncs.ecs.model.v20140526.DescribeAvailableResourceRequest; +import com.aliyuncs.ecs.model.v20140526.DescribeAvailableResourceResponse; +import com.aliyuncs.ecs.model.v20140526.DescribeAvailableResourceResponse.AvailableZone; +import com.aliyuncs.ecs.model.v20140526.DescribeAvailableResourceResponse.AvailableZone.AvailableResource; +import com.aliyuncs.ecs.model.v20140526.DescribeAvailableResourceResponse.AvailableZone.AvailableResource.SupportedResource; +import com.aliyuncs.exceptions.ClientException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.netflix.spinnaker.cats.agent.AgentDataType; +import com.netflix.spinnaker.cats.agent.CacheResult; +import com.netflix.spinnaker.cats.agent.CachingAgent; +import com.netflix.spinnaker.cats.agent.DefaultCacheResult; +import com.netflix.spinnaker.cats.cache.CacheData; +import com.netflix.spinnaker.cats.cache.DefaultCacheData; +import com.netflix.spinnaker.cats.provider.ProviderCache; +import com.netflix.spinnaker.clouddriver.alicloud.AliCloudProvider; +import com.netflix.spinnaker.clouddriver.alicloud.cache.Keys; +import com.netflix.spinnaker.clouddriver.alicloud.provider.AliProvider; +import com.netflix.spinnaker.clouddriver.alicloud.security.AliCloudCredentials; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +public class AliCloudInstanceTypeCachingAgent implements CachingAgent { + + AliCloudCredentials account; + String region; + ObjectMapper objectMapper; + IAcsClient client; + + public AliCloudInstanceTypeCachingAgent( + AliCloudCredentials account, String region, ObjectMapper objectMapper, IAcsClient client) { + this.account = account; + this.region = region; + this.objectMapper = objectMapper; + this.client = client; + } + + static final Collection types = + Collections.unmodifiableCollection( + new ArrayList<>() { + { + add(AUTHORITATIVE.forType(Keys.Namespace.INSTANCE_TYPES.ns)); + } + }); + + @Override + public CacheResult loadData(ProviderCache providerCache) { + + List zones = findAllAvailableResources(); + if (zones == null) { + return new DefaultCacheResult(new HashMap<>(16)); + } + + List instanceTypeDatas = + zones.stream() + .map( + availableZone -> { + String regionId = availableZone.getBizRegionId(); + String zoneId = availableZone.getZoneId(); + List names = findAvailableResourceNames(availableZone); + + Map attributes = new HashMap<>(20); + attributes.put("provider", AliCloudProvider.ID); + attributes.put("account", account.getName()); + attributes.put("regionId", regionId); + attributes.put("zoneId", zoneId); + attributes.put("names", names); + return (CacheData) + new DefaultCacheData( + Keys.getInstanceTypeKey(account.getName(), region, zoneId), + attributes, + new HashMap<>(16)); + }) + .collect(Collectors.toList()); + + Map> resultMap = new HashMap<>(16); + resultMap.put(Keys.Namespace.INSTANCE_TYPES.ns, instanceTypeDatas); + return new DefaultCacheResult(resultMap); + } + + private List findAvailableResourceNames(AvailableZone availableZone) { + if (!"Available".equals(availableZone.getStatus())) { + return Collections.emptyList(); + } + if ("WithoutStock".equals(availableZone.getStatusCategory())) { + return Collections.emptyList(); + } + return availableZone.getAvailableResources().stream() + .filter(r -> "InstanceType".equals(r.getType())) + .map(AvailableResource::getSupportedResources) + .flatMap(Collection::stream) + .filter(supportedResource -> "Available".equals(supportedResource.getStatus())) + .filter(supportedResource -> !"WithoutStock".equals(supportedResource.getStatusCategory())) + .map(SupportedResource::getValue) + .collect(Collectors.toList()); + } + + private List findAllAvailableResources() { + + DescribeAvailableResourceRequest describeZonesRequest = new DescribeAvailableResourceRequest(); + describeZonesRequest.setDestinationResource("InstanceType"); + describeZonesRequest.setInstanceChargeType("PostPaid"); + describeZonesRequest.setIoOptimized("optimized"); + describeZonesRequest.setResourceType("instance"); + + DescribeAvailableResourceResponse describeZonesResponse; + try { + describeZonesResponse = client.getAcsResponse(describeZonesRequest); + return describeZonesResponse.getAvailableZones(); + + } catch (ClientException e) { + e.printStackTrace(); + } + return null; + } + + @Override + public Collection getProvidedDataTypes() { + return types; + } + + @Override + public String getAgentType() { + return account.getName() + "/" + region + "/" + this.getClass().getSimpleName(); + } + + @Override + public String getProviderName() { + return AliProvider.PROVIDER_NAME; + } +} diff --git a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/provider/agent/AliCloudKeyPairCachingAgent.java b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/provider/agent/AliCloudKeyPairCachingAgent.java new file mode 100644 index 00000000000..ff37f160a1c --- /dev/null +++ b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/provider/agent/AliCloudKeyPairCachingAgent.java @@ -0,0 +1,123 @@ +/* + * Copyright 2022 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.clouddriver.alicloud.provider.agent; + +import static com.netflix.spinnaker.cats.agent.AgentDataType.Authority.AUTHORITATIVE; + +import com.aliyuncs.IAcsClient; +import com.aliyuncs.ecs.model.v20140526.DescribeKeyPairsRequest; +import com.aliyuncs.ecs.model.v20140526.DescribeKeyPairsResponse; +import com.aliyuncs.ecs.model.v20140526.DescribeKeyPairsResponse.KeyPair; +import com.aliyuncs.exceptions.ClientException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.netflix.spinnaker.cats.agent.AgentDataType; +import com.netflix.spinnaker.cats.agent.CacheResult; +import com.netflix.spinnaker.cats.agent.CachingAgent; +import com.netflix.spinnaker.cats.agent.DefaultCacheResult; +import com.netflix.spinnaker.cats.cache.CacheData; +import com.netflix.spinnaker.cats.cache.DefaultCacheData; +import com.netflix.spinnaker.cats.provider.ProviderCache; +import com.netflix.spinnaker.clouddriver.alicloud.AliCloudProvider; +import com.netflix.spinnaker.clouddriver.alicloud.cache.Keys; +import com.netflix.spinnaker.clouddriver.alicloud.provider.AliProvider; +import com.netflix.spinnaker.clouddriver.alicloud.security.AliCloudCredentials; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +public class AliCloudKeyPairCachingAgent implements CachingAgent { + + AliCloudCredentials account; + String region; + ObjectMapper objectMapper; + IAcsClient client; + + public AliCloudKeyPairCachingAgent( + AliCloudCredentials account, String region, ObjectMapper objectMapper, IAcsClient client) { + this.account = account; + this.region = region; + this.objectMapper = objectMapper; + this.client = client; + } + + static final Collection types = + Collections.unmodifiableCollection( + new ArrayList<>() { + { + add(AUTHORITATIVE.forType(Keys.Namespace.ALI_CLOUD_KEY_PAIRS.ns)); + } + }); + + @Override + public CacheResult loadData(ProviderCache providerCache) { + + List keyPairs = findKeyPairs(); + if (keyPairs == null) { + return new DefaultCacheResult(new HashMap<>(16)); + } + + List keyPairDatas = + keyPairs.stream() + .map( + keyPair -> { + Map attributes = objectMapper.convertValue(keyPair, Map.class); + attributes.put("provider", AliCloudProvider.ID); + attributes.put("account", account.getName()); + attributes.put("regionId", region); + return new DefaultCacheData( + Keys.getKeyPairKey(keyPair.getKeyPairName(), region, account.getName()), + attributes, + new HashMap<>(16)); + }) + .collect(Collectors.toList()); + + Map> resultMap = new HashMap<>(16); + resultMap.put(Keys.Namespace.ALI_CLOUD_KEY_PAIRS.ns, keyPairDatas); + return new DefaultCacheResult(resultMap); + } + + private List findKeyPairs() { + DescribeKeyPairsRequest keyPairsRequest = new DescribeKeyPairsRequest(); + keyPairsRequest.setPageSize(50); + DescribeKeyPairsResponse keyPairsResponse; + try { + keyPairsResponse = client.getAcsResponse(keyPairsRequest); + return keyPairsResponse.getKeyPairs(); + } catch (ClientException e) { + e.printStackTrace(); + } + return Collections.emptyList(); + } + + @Override + public String getAgentType() { + return account.getName() + "/" + region + "/" + this.getClass().getSimpleName(); + } + + @Override + public String getProviderName() { + return AliProvider.PROVIDER_NAME; + } + + @Override + public Collection getProvidedDataTypes() { + return types; + } +} diff --git a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/provider/agent/AliCloudLoadBalancerCachingAgent.java b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/provider/agent/AliCloudLoadBalancerCachingAgent.java index 0a6dd0f950b..a0c14abbe25 100644 --- a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/provider/agent/AliCloudLoadBalancerCachingAgent.java +++ b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/provider/agent/AliCloudLoadBalancerCachingAgent.java @@ -5,7 +5,7 @@ * 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 + * 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, @@ -17,7 +17,9 @@ import static com.netflix.spinnaker.cats.agent.AgentDataType.Authority.AUTHORITATIVE; import static com.netflix.spinnaker.cats.agent.AgentDataType.Authority.INFORMATIVE; -import static com.netflix.spinnaker.clouddriver.core.provider.agent.Namespace.*; +import static com.netflix.spinnaker.clouddriver.core.provider.agent.Namespace.INSTANCES; +import static com.netflix.spinnaker.clouddriver.core.provider.agent.Namespace.LOAD_BALANCERS; +import static com.netflix.spinnaker.clouddriver.core.provider.agent.Namespace.ON_DEMAND; import com.aliyuncs.IAcsClient; import com.aliyuncs.exceptions.ClientException; @@ -25,25 +27,42 @@ import com.aliyuncs.slb.model.v20140515.*; import com.aliyuncs.slb.model.v20140515.DescribeLoadBalancerAttributeResponse.ListenerPortAndProtocal; import com.aliyuncs.slb.model.v20140515.DescribeLoadBalancersResponse.LoadBalancer; +import com.aliyuncs.slb.model.v20140515.DescribeVServerGroupsResponse.VServerGroup; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.google.common.collect.Lists; import com.google.common.collect.Maps; import com.netflix.spectator.api.Registry; -import com.netflix.spinnaker.cats.agent.*; +import com.netflix.spinnaker.cats.agent.AccountAware; +import com.netflix.spinnaker.cats.agent.AgentDataType; +import com.netflix.spinnaker.cats.agent.CacheResult; +import com.netflix.spinnaker.cats.agent.CachingAgent; +import com.netflix.spinnaker.cats.agent.DefaultCacheResult; import com.netflix.spinnaker.cats.cache.CacheData; import com.netflix.spinnaker.cats.cache.DefaultCacheData; import com.netflix.spinnaker.cats.provider.ProviderCache; import com.netflix.spinnaker.clouddriver.alicloud.AliCloudProvider; import com.netflix.spinnaker.clouddriver.alicloud.cache.Keys; +import com.netflix.spinnaker.clouddriver.alicloud.model.AliCloudLoadBalancer; import com.netflix.spinnaker.clouddriver.alicloud.provider.AliProvider; import com.netflix.spinnaker.clouddriver.alicloud.security.AliCloudClientProvider; import com.netflix.spinnaker.clouddriver.alicloud.security.AliCloudCredentials; import com.netflix.spinnaker.clouddriver.alicloud.security.AliCloudCredentialsProvider; import com.netflix.spinnaker.clouddriver.cache.OnDemandAgent; import com.netflix.spinnaker.clouddriver.cache.OnDemandMetricsSupport; -import java.util.*; +import com.netflix.spinnaker.clouddriver.cache.OnDemandType; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.Date; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.stream.Collectors; +import lombok.Data; import org.apache.commons.lang3.StringUtils; +import org.jetbrains.annotations.NotNull; public class AliCloudLoadBalancerCachingAgent implements CachingAgent, AccountAware, OnDemandAgent { @@ -80,13 +99,13 @@ public AliCloudLoadBalancerCachingAgent( + ":" + aliCloudProvider.getId() + ":" - + OnDemandAgent.OnDemandType.LoadBalancer); + + OnDemandType.LoadBalancer); this.client = client; } static final Collection types = Collections.unmodifiableCollection( - new ArrayList() { + new ArrayList<>() { { add(AUTHORITATIVE.forType(LOAD_BALANCERS.ns)); add(INFORMATIVE.forType(INSTANCES.ns)); @@ -95,7 +114,7 @@ public AliCloudLoadBalancerCachingAgent( @Override public String getAccountName() { - return null; + return account.getName(); } @Override @@ -105,50 +124,148 @@ public Collection getProvidedDataTypes() { @Override public CacheResult loadData(ProviderCache providerCache) { - List loadBalancers = new ArrayList<>(); - Map loadBalancerAttributes = new HashMap<>(16); - - DescribeLoadBalancersRequest queryRequest = new DescribeLoadBalancersRequest(); - DescribeLoadBalancersResponse queryResponse; + DescribeLoadBalancersResponse queryResponse = null; try { + DescribeLoadBalancersRequest queryRequest = new DescribeLoadBalancersRequest(); queryResponse = client.getAcsResponse(queryRequest); - if (queryResponse.getLoadBalancers().isEmpty()) { - return new DefaultCacheResult(new HashMap<>(16)); - } + } catch (ClientException e) { + e.printStackTrace(); + } - loadBalancers.addAll(queryResponse.getLoadBalancers()); + if (queryResponse == null + || queryResponse.getLoadBalancers() == null + || queryResponse.getLoadBalancers().isEmpty()) { + return new DefaultCacheResult(new HashMap<>(16)); + } - } catch (ServerException e) { - e.printStackTrace(); + return buildCacheResult(queryResponse); + } + + @NotNull + private DefaultCacheResult buildCacheResult(DescribeLoadBalancersResponse queryResponse) { + List defaultCacheDatas = + queryResponse.getLoadBalancers().stream() + .map(this::getLoadBalancerCacheBuilder) + .map(detail -> detail.buildLoadBalancerCache(objectMapper)) + .collect(Collectors.toList()); + + Map> cacheResults = new HashMap<>(16); + cacheResults.put(LOAD_BALANCERS.ns, defaultCacheDatas); + return new DefaultCacheResult(cacheResults); + } + + @NotNull + private LoadBalancerCacheBuilder getLoadBalancerCacheBuilder(LoadBalancer loadBalancer) { + String loadBalancerId = loadBalancer.getLoadBalancerId(); + + DescribeLoadBalancerAttributeRequest describeLoadBalancerAttributeRequest = + new DescribeLoadBalancerAttributeRequest(); + describeLoadBalancerAttributeRequest.setLoadBalancerId(loadBalancerId); + DescribeLoadBalancerAttributeResponse loadBalancerAttribute = null; + List listenerAttributes = null; + try { + loadBalancerAttribute = client.getAcsResponse(describeLoadBalancerAttributeRequest); + List listenerPortsAndProtocal = + loadBalancerAttribute.getListenerPortsAndProtocal(); + + listenerAttributes = + listenerPortsAndProtocal.stream() + .map( + portAndProtocal -> + describeLoadBalancerListenerAttribute(loadBalancerId, portAndProtocal)) + .filter(Objects::nonNull) + .collect(Collectors.toList()); } catch (ClientException e) { e.printStackTrace(); } - for (LoadBalancer loadBalancer : loadBalancers) { - - DescribeLoadBalancerAttributeRequest describeLoadBalancerAttributeRequest = - new DescribeLoadBalancerAttributeRequest(); - describeLoadBalancerAttributeRequest.setLoadBalancerId(loadBalancer.getLoadBalancerId()); - DescribeLoadBalancerAttributeResponse describeLoadBalancerAttributeResponse; - try { - describeLoadBalancerAttributeResponse = - client.getAcsResponse(describeLoadBalancerAttributeRequest); - loadBalancerAttributes.put( - loadBalancer.getLoadBalancerName(), describeLoadBalancerAttributeResponse); - - } catch (ServerException e) { - e.printStackTrace(); - } catch (ClientException e) { - e.printStackTrace(); - } + List vServerGroups = null; + try { + DescribeVServerGroupsRequest describeVServerGroupsRequest = + new DescribeVServerGroupsRequest(); + describeVServerGroupsRequest.setLoadBalancerId(loadBalancerId); + DescribeVServerGroupsResponse describeVServerGroupsResponse = + client.getAcsResponse(describeVServerGroupsRequest); + vServerGroups = describeVServerGroupsResponse.getVServerGroups(); + } catch (ClientException e) { + e.printStackTrace(); } - return buildCacheResult(loadBalancers, loadBalancerAttributes, client); + return new LoadBalancerCacheBuilder( + loadBalancer, + loadBalancerAttribute, + listenerAttributes, + vServerGroups, + account.getName(), + region); + } + + private Map describeLoadBalancerListenerAttribute( + String loadBalancerId, ListenerPortAndProtocal portAndProtocal) { + Integer listenerPort = portAndProtocal.getListenerPort(); + String listenerProtocal = portAndProtocal.getListenerProtocal().toUpperCase(); + switch (listenerProtocal) { + case "HTTPS": + DescribeLoadBalancerHTTPSListenerAttributeRequest httpsListenerAttributeRequest = + new DescribeLoadBalancerHTTPSListenerAttributeRequest(); + httpsListenerAttributeRequest.setListenerPort(listenerPort); + httpsListenerAttributeRequest.setLoadBalancerId(loadBalancerId); + DescribeLoadBalancerHTTPSListenerAttributeResponse httpsListenerAttributeResponse; + try { + httpsListenerAttributeResponse = client.getAcsResponse(httpsListenerAttributeRequest); + return objectMapper.convertValue(httpsListenerAttributeResponse, Map.class); + } catch (ClientException e) { + e.printStackTrace(); + } + break; + case "TCP": + DescribeLoadBalancerTCPListenerAttributeRequest tcpListenerAttributeRequest = + new DescribeLoadBalancerTCPListenerAttributeRequest(); + tcpListenerAttributeRequest.setListenerPort(listenerPort); + tcpListenerAttributeRequest.setLoadBalancerId(loadBalancerId); + DescribeLoadBalancerTCPListenerAttributeResponse tcpListenerAttributeResponse; + try { + tcpListenerAttributeResponse = client.getAcsResponse(tcpListenerAttributeRequest); + return objectMapper.convertValue(tcpListenerAttributeResponse, Map.class); + } catch (ClientException e) { + e.printStackTrace(); + } + + break; + case "UDP": + DescribeLoadBalancerUDPListenerAttributeRequest udpListenerAttributeRequest = + new DescribeLoadBalancerUDPListenerAttributeRequest(); + udpListenerAttributeRequest.setListenerPort(listenerPort); + udpListenerAttributeRequest.setLoadBalancerId(loadBalancerId); + DescribeLoadBalancerUDPListenerAttributeResponse udpListenerAttributeResponse; + try { + udpListenerAttributeResponse = client.getAcsResponse(udpListenerAttributeRequest); + return objectMapper.convertValue(udpListenerAttributeResponse, Map.class); + } catch (ClientException e) { + e.printStackTrace(); + } + + break; + default: + DescribeLoadBalancerHTTPListenerAttributeRequest httpListenerAttributeRequest = + new DescribeLoadBalancerHTTPListenerAttributeRequest(); + httpListenerAttributeRequest.setListenerPort(listenerPort); + httpListenerAttributeRequest.setLoadBalancerId(loadBalancerId); + DescribeLoadBalancerHTTPListenerAttributeResponse httpListenerAttributeResponse; + try { + httpListenerAttributeResponse = client.getAcsResponse(httpListenerAttributeRequest); + return objectMapper.convertValue(httpListenerAttributeResponse, Map.class); + } catch (ClientException e) { + e.printStackTrace(); + } + break; + } + return null; } @Override public OnDemandResult handle(ProviderCache providerCache, Map data) { - List loadBalancers = new ArrayList<>(); + List loadBalancers = new ArrayList<>(); Map loadBalancerAttributes = new HashMap<>(16); DescribeLoadBalancersRequest queryRequest = new DescribeLoadBalancersRequest(); @@ -193,11 +310,7 @@ public OnDemandResult handle(ProviderCache providerCache, Map loadBalancers, - Map loadBalancerAttributes, - IAcsClient client) { - - Map> cacheResults = new HashMap<>(16); - List list = new ArrayList(); - - for (LoadBalancer loadBalancer : loadBalancers) { - String loadBalancerName = loadBalancer.getLoadBalancerName(); - Map map = objectMapper.convertValue(loadBalancer, Map.class); - map.put("account", account.getName()); - - DescribeLoadBalancerAttributeResponse describeLoadBalancerAttributeResponse = - loadBalancerAttributes.get(loadBalancerName); - Map attributeMap = - objectMapper.convertValue(describeLoadBalancerAttributeResponse, Map.class); - - List listenerPortsAndProtocal = new ArrayList<>(); - for (ListenerPortAndProtocal listenerPortAndProtocal : - describeLoadBalancerAttributeResponse.getListenerPortsAndProtocal()) { - Integer listenerPort = listenerPortAndProtocal.getListenerPort(); - String listenerProtocal = listenerPortAndProtocal.getListenerProtocal().toUpperCase(); - Map portAndProtocalMap = - objectMapper.convertValue(listenerPortAndProtocal, Map.class); - - Map listenerMap = new HashMap<>(16); - - switch (listenerProtocal) { - case "HTTPS": - DescribeLoadBalancerHTTPSListenerAttributeRequest httpsListenerAttributeRequest = - new DescribeLoadBalancerHTTPSListenerAttributeRequest(); - httpsListenerAttributeRequest.setListenerPort(listenerPort); - httpsListenerAttributeRequest.setLoadBalancerId(loadBalancer.getLoadBalancerId()); - DescribeLoadBalancerHTTPSListenerAttributeResponse httpsListenerAttributeResponse; - try { - httpsListenerAttributeResponse = client.getAcsResponse(httpsListenerAttributeRequest); - listenerMap = objectMapper.convertValue(httpsListenerAttributeResponse, Map.class); - } catch (ServerException e) { - e.printStackTrace(); - } catch (ClientException e) { - e.printStackTrace(); - } - - break; - case "TCP": - DescribeLoadBalancerTCPListenerAttributeRequest tcpListenerAttributeRequest = - new DescribeLoadBalancerTCPListenerAttributeRequest(); - tcpListenerAttributeRequest.setListenerPort(listenerPort); - tcpListenerAttributeRequest.setLoadBalancerId(loadBalancer.getLoadBalancerId()); - DescribeLoadBalancerTCPListenerAttributeResponse tcpListenerAttributeResponse; - try { - tcpListenerAttributeResponse = client.getAcsResponse(tcpListenerAttributeRequest); - listenerMap = objectMapper.convertValue(tcpListenerAttributeResponse, Map.class); - } catch (ServerException e) { - e.printStackTrace(); - } catch (ClientException e) { - e.printStackTrace(); - } - - break; - case "UDP": - DescribeLoadBalancerUDPListenerAttributeRequest udpListenerAttributeRequest = - new DescribeLoadBalancerUDPListenerAttributeRequest(); - udpListenerAttributeRequest.setListenerPort(listenerPort); - udpListenerAttributeRequest.setLoadBalancerId(loadBalancer.getLoadBalancerId()); - DescribeLoadBalancerUDPListenerAttributeResponse udpListenerAttributeResponse; - try { - udpListenerAttributeResponse = client.getAcsResponse(udpListenerAttributeRequest); - listenerMap = objectMapper.convertValue(udpListenerAttributeResponse, Map.class); - } catch (ServerException e) { - e.printStackTrace(); - } catch (ClientException e) { - e.printStackTrace(); - } - - break; - default: - DescribeLoadBalancerHTTPListenerAttributeRequest httpListenerAttributeRequest = - new DescribeLoadBalancerHTTPListenerAttributeRequest(); - httpListenerAttributeRequest.setListenerPort(listenerPort); - httpListenerAttributeRequest.setLoadBalancerId(loadBalancer.getLoadBalancerId()); - DescribeLoadBalancerHTTPListenerAttributeResponse httpListenerAttributeResponse; - try { - httpListenerAttributeResponse = client.getAcsResponse(httpListenerAttributeRequest); - listenerMap = objectMapper.convertValue(httpListenerAttributeResponse, Map.class); - } catch (ServerException e) { - e.printStackTrace(); - } catch (ClientException e) { - e.printStackTrace(); - } - break; - } - listenerMap.putAll(portAndProtocalMap); - listenerPortsAndProtocal.add(listenerMap); - } - - attributeMap.put("listenerPortsAndProtocal", listenerPortsAndProtocal); - map.put("attributes", attributeMap); - DescribeVServerGroupsRequest describeVServerGroupsRequest = - new DescribeVServerGroupsRequest(); - describeVServerGroupsRequest.setLoadBalancerId(loadBalancer.getLoadBalancerId()); - try { - DescribeVServerGroupsResponse describeVServerGroupsResponse = - client.getAcsResponse(describeVServerGroupsRequest); - List vServerGroups = - describeVServerGroupsResponse.getVServerGroups(); - map.put("vServerGroups", vServerGroups); - } catch (ServerException e) { - e.printStackTrace(); - } catch (ClientException e) { - e.printStackTrace(); - } - list.add( - new DefaultCacheData( - Keys.getLoadBalancerKey( - loadBalancerName, account.getName(), region, loadBalancer.getVpcId()), - map, - Maps.newHashMap())); - } - cacheResults.put(LOAD_BALANCERS.ns, list); - - CacheResult cacheResult = new DefaultCacheResult(cacheResults); - - return cacheResult; - } - @Override public String getAgentType() { return account.getName() + "/" + region + "/" + this.getClass().getSimpleName(); @@ -393,7 +379,64 @@ public boolean handles(OnDemandType type, String cloudProvider) { } @Override - public Collection pendingOnDemandRequests(ProviderCache providerCache) { + public Collection> pendingOnDemandRequests(ProviderCache providerCache) { return null; } + + @Data + public static class LoadBalancerCacheBuilder { + final LoadBalancer loadBalancer; + final DescribeLoadBalancerAttributeResponse loadBalancerAttribute; + final List listenerAttributes; + final List vServerGroups; + final String account; + final String region; + + public LoadBalancerCacheBuilder( + LoadBalancer loadBalancer, + DescribeLoadBalancerAttributeResponse loadBalancerAttribute, + List listenerAttributes, + List vServerGroups, + String account, + String region) { + this.loadBalancer = loadBalancer; + this.loadBalancerAttribute = loadBalancerAttribute; + this.listenerAttributes = listenerAttributes; + this.vServerGroups = vServerGroups; + this.account = account; + this.region = region; + } + + public DefaultCacheData buildLoadBalancerCache(ObjectMapper objectMapper) { + LoadBalancer loadBalancer = this.loadBalancer; + Map map = objectMapper.convertValue(loadBalancer, Map.class); + map.put("account", this.account); + Map attributeMap = + objectMapper.convertValue(this.loadBalancerAttribute, Map.class); + attributeMap.put("listenerPortsAndProtocal", this.listenerAttributes); + map.put("attributes", attributeMap); + map.put("vServerGroups", this.vServerGroups); + + return new DefaultCacheData( + Keys.getLoadBalancerKey( + loadBalancer.getLoadBalancerName(), + this.account, + this.region, + loadBalancer.getVpcId()), + map, + Maps.newHashMap()); + } + + public static AliCloudLoadBalancer buildLoadBalancer( + CacheData loadBalancerCacheData, ObjectMapper objectMapper) { + Map attributes = + objectMapper.convertValue(loadBalancerCacheData.getAttributes(), Map.class); + return new AliCloudLoadBalancer( + String.valueOf(attributes.get("account")), + String.valueOf(attributes.get("regionIdAlias")), + String.valueOf(attributes.get("loadBalancerName")), + String.valueOf(attributes.get("vpcId")), + String.valueOf(attributes.get("loadBalancerId"))); + } + } } diff --git a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/provider/agent/AliCloudLoadBalancerInstanceStateCachingAgent.java b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/provider/agent/AliCloudLoadBalancerInstanceStateCachingAgent.java index 65de8a5a171..7995eb16d4c 100644 --- a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/provider/agent/AliCloudLoadBalancerInstanceStateCachingAgent.java +++ b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/provider/agent/AliCloudLoadBalancerInstanceStateCachingAgent.java @@ -16,14 +16,13 @@ package com.netflix.spinnaker.clouddriver.alicloud.provider.agent; import static com.netflix.spinnaker.cats.agent.AgentDataType.Authority.AUTHORITATIVE; -import static com.netflix.spinnaker.clouddriver.core.provider.agent.Namespace.HEALTH; -import static com.netflix.spinnaker.clouddriver.core.provider.agent.Namespace.LOAD_BALANCERS; +import static com.netflix.spinnaker.clouddriver.core.provider.agent.Namespace.*; import com.aliyuncs.IAcsClient; import com.aliyuncs.exceptions.ClientException; -import com.aliyuncs.exceptions.ServerException; import com.aliyuncs.slb.model.v20140515.DescribeHealthStatusRequest; import com.aliyuncs.slb.model.v20140515.DescribeHealthStatusResponse; +import com.aliyuncs.slb.model.v20140515.DescribeHealthStatusResponse.BackendServer; import com.fasterxml.jackson.databind.ObjectMapper; import com.netflix.spinnaker.cats.agent.AgentDataType; import com.netflix.spinnaker.cats.agent.CacheResult; @@ -65,7 +64,7 @@ public AliCloudLoadBalancerInstanceStateCachingAgent( static final Collection types = Collections.unmodifiableCollection( - new ArrayList() { + new ArrayList<>() { { add(AUTHORITATIVE.forType(HEALTH.ns)); } @@ -73,52 +72,57 @@ public AliCloudLoadBalancerInstanceStateCachingAgent( @Override public CacheResult loadData(ProviderCache providerCache) { - Map> resultMap = new HashMap<>(16); - List instanceDatas = new ArrayList<>(); Collection allLoadBalancerKeys = getCacheView().getIdentifiers(LOAD_BALANCERS.ns); Collection loadBalancerData = getCacheView().getAll(LOAD_BALANCERS.ns, allLoadBalancerKeys, null); - DescribeHealthStatusRequest describeHealthStatusRequest = new DescribeHealthStatusRequest(); - DescribeHealthStatusResponse describeHealthStatusResponse; + + List instanceDatas = new ArrayList<>(); + for (CacheData cacheData : loadBalancerData) { - Map loadBalancerAttributes = - objectMapper.convertValue(cacheData.getAttributes(), Map.class); + Map loadBalancerAttributes = cacheData.getAttributes(); String loadBalancerId = String.valueOf(loadBalancerAttributes.get("loadBalancerId")); - describeHealthStatusRequest.setLoadBalancerId(loadBalancerId); String regionId = String.valueOf(loadBalancerAttributes.get("regionId")); - describeHealthStatusRequest.setSysRegionId(regionId); - try { - describeHealthStatusResponse = client.getAcsResponse(describeHealthStatusRequest); - for (DescribeHealthStatusResponse.BackendServer backendServer : - describeHealthStatusResponse.getBackendServers()) { - Map attributes = objectMapper.convertValue(backendServer, Map.class); - attributes.put("loadBalancerId", loadBalancerId); - CacheData data = - new DefaultCacheData( - Keys.getInstanceHealthKey( - loadBalancerId, - backendServer.getServerId(), - backendServer.getListenerPort().toString(), - account.getName(), - regionId, - healthId), - attributes, - new HashMap<>(16)); - instanceDatas.add(data); - } - - } catch (ServerException e) { - e.printStackTrace(); - } catch (ClientException e) { - e.printStackTrace(); + + List backendServers = this.findBackendServer(loadBalancerId, regionId); + for (DescribeHealthStatusResponse.BackendServer backendServer : backendServers) { + Map attributes = objectMapper.convertValue(backendServer, Map.class); + attributes.put("loadBalancerId", loadBalancerId); + CacheData data = + new DefaultCacheData( + Keys.getInstanceHealthKey( + loadBalancerId, + backendServer.getServerId(), + backendServer.getListenerPort().toString(), + account.getName(), + regionId, + healthId), + attributes, + new HashMap<>(16)); + instanceDatas.add(data); } } + + Map> resultMap = new HashMap<>(16); resultMap.put(HEALTH.ns, instanceDatas); return new DefaultCacheResult(resultMap); } + private List findBackendServer(String loadBalancerId, String regionId) { + DescribeHealthStatusRequest describeHealthStatusRequest = new DescribeHealthStatusRequest(); + DescribeHealthStatusResponse describeHealthStatusResponse; + try { + describeHealthStatusRequest.setLoadBalancerId(loadBalancerId); + describeHealthStatusRequest.setSysRegionId(regionId); + describeHealthStatusResponse = client.getAcsResponse(describeHealthStatusRequest); + return describeHealthStatusResponse.getBackendServers(); + } catch (ClientException e) { + e.printStackTrace(); + } + return Collections.emptyList(); + } + @Override public Collection getProvidedDataTypes() { return types; diff --git a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/provider/agent/AliCloudSecurityGroupCachingAgent.java b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/provider/agent/AliCloudSecurityGroupCachingAgent.java index 701c9782236..7b2f27743e5 100644 --- a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/provider/agent/AliCloudSecurityGroupCachingAgent.java +++ b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/provider/agent/AliCloudSecurityGroupCachingAgent.java @@ -5,7 +5,7 @@ * 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 + * 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, @@ -24,9 +24,12 @@ import com.aliyuncs.ecs.model.v20140526.DescribeSecurityGroupsResponse; import com.aliyuncs.ecs.model.v20140526.DescribeSecurityGroupsResponse.SecurityGroup; import com.aliyuncs.exceptions.ClientException; -import com.aliyuncs.exceptions.ServerException; import com.fasterxml.jackson.databind.ObjectMapper; -import com.netflix.spinnaker.cats.agent.*; +import com.netflix.spinnaker.cats.agent.AccountAware; +import com.netflix.spinnaker.cats.agent.AgentDataType; +import com.netflix.spinnaker.cats.agent.CacheResult; +import com.netflix.spinnaker.cats.agent.CachingAgent; +import com.netflix.spinnaker.cats.agent.DefaultCacheResult; import com.netflix.spinnaker.cats.cache.CacheData; import com.netflix.spinnaker.cats.cache.DefaultCacheData; import com.netflix.spinnaker.cats.provider.ProviderCache; @@ -36,7 +39,13 @@ import com.netflix.spinnaker.clouddriver.alicloud.security.AliCloudCredentials; import com.netflix.spinnaker.clouddriver.cache.OnDemandAgent; import com.netflix.spinnaker.clouddriver.cache.OnDemandMetricsSupport; -import java.util.*; +import com.netflix.spinnaker.clouddriver.cache.OnDemandType; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; public class AliCloudSecurityGroupCachingAgent implements CachingAgent, OnDemandAgent, AccountAware { @@ -56,7 +65,7 @@ public AliCloudSecurityGroupCachingAgent( static final Collection types = Collections.unmodifiableCollection( - new ArrayList() { + new ArrayList<>() { { add(AUTHORITATIVE.forType(Keys.Namespace.SECURITY_GROUPS.ns)); } @@ -77,8 +86,6 @@ public CacheResult loadData(ProviderCache providerCache) { securityGroupDatas.add(buildCatchData(securityGroup)); } - } catch (ServerException e) { - e.printStackTrace(); } catch (ClientException e) { e.printStackTrace(); } @@ -115,8 +122,7 @@ public OnDemandMetricsSupport getMetricsSupport() { @Override public boolean handles(OnDemandType type, String cloudProvider) { - return OnDemandAgent.OnDemandType.SecurityGroup.equals(type) - && AliCloudProvider.ID.equals(cloudProvider); + return OnDemandType.SecurityGroup.equals(type) && AliCloudProvider.ID.equals(cloudProvider); } @Override @@ -138,15 +144,13 @@ public OnDemandResult handle(ProviderCache providerCache, Map attributes = objectMapper.convertValue(securityGroup, Map.class); attributes.put("provider", AliCloudProvider.ID); attributes.put("account", account.getName()); @@ -171,8 +175,8 @@ CacheData buildCatchData(SecurityGroup securityGroup) throws ClientException, Se } @Override - public Collection pendingOnDemandRequests(ProviderCache providerCache) { - List resultList = new ArrayList<>(); + public Collection> pendingOnDemandRequests(ProviderCache providerCache) { + List> resultList = new ArrayList<>(); Map map = new HashMap<>(); resultList.add(map); return resultList; diff --git a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/provider/agent/AliCloudSubnetCachingAgent.java b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/provider/agent/AliCloudSubnetCachingAgent.java new file mode 100644 index 00000000000..863e060776b --- /dev/null +++ b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/provider/agent/AliCloudSubnetCachingAgent.java @@ -0,0 +1,153 @@ +/* + * Copyright 2022 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.clouddriver.alicloud.provider.agent; + +import static com.netflix.spinnaker.cats.agent.AgentDataType.Authority.AUTHORITATIVE; +import static com.netflix.spinnaker.clouddriver.alicloud.cache.Keys.Namespace.SUBNETS; + +import com.aliyuncs.IAcsClient; +import com.aliyuncs.exceptions.ClientException; +import com.aliyuncs.vpc.model.v20160428.DescribeVSwitchesRequest; +import com.aliyuncs.vpc.model.v20160428.DescribeVSwitchesResponse; +import com.aliyuncs.vpc.model.v20160428.DescribeVSwitchesResponse.VSwitch; +import com.aliyuncs.vpc.model.v20160428.DescribeVpcsRequest; +import com.aliyuncs.vpc.model.v20160428.DescribeVpcsResponse; +import com.aliyuncs.vpc.model.v20160428.DescribeVpcsResponse.Vpc; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.netflix.spinnaker.cats.agent.AccountAware; +import com.netflix.spinnaker.cats.agent.AgentDataType; +import com.netflix.spinnaker.cats.agent.CacheResult; +import com.netflix.spinnaker.cats.agent.CachingAgent; +import com.netflix.spinnaker.cats.agent.DefaultCacheResult; +import com.netflix.spinnaker.cats.cache.CacheData; +import com.netflix.spinnaker.cats.cache.DefaultCacheData; +import com.netflix.spinnaker.cats.provider.ProviderCache; +import com.netflix.spinnaker.clouddriver.alicloud.AliCloudProvider; +import com.netflix.spinnaker.clouddriver.alicloud.cache.Keys; +import com.netflix.spinnaker.clouddriver.alicloud.provider.AliProvider; +import com.netflix.spinnaker.clouddriver.alicloud.security.AliCloudCredentials; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import lombok.Data; + +public class AliCloudSubnetCachingAgent implements CachingAgent, AccountAware { + + AliCloudCredentials account; + String region; + ObjectMapper objectMapper; + IAcsClient client; + + public AliCloudSubnetCachingAgent( + AliCloudCredentials account, String region, ObjectMapper objectMapper, IAcsClient client) { + this.account = account; + this.region = region; + this.objectMapper = objectMapper; + this.client = client; + } + + static final Collection types = + Collections.unmodifiableCollection( + new ArrayList<>() { + { + add(AUTHORITATIVE.forType(SUBNETS.ns)); + } + }); + + @Override + public CacheResult loadData(ProviderCache providerCache) { + + Map> resultMap = new HashMap<>(16); + List datas = new ArrayList<>(); + DescribeVSwitchesRequest describeVSwitchesRequest = new DescribeVSwitchesRequest(); + describeVSwitchesRequest.setPageSize(50); + DescribeVSwitchesResponse describeVSwitchesResponse; + try { + describeVSwitchesResponse = client.getAcsResponse(describeVSwitchesRequest); + for (VSwitch vSwitch : describeVSwitchesResponse.getVSwitches()) { + DescribeVpcsRequest describeVpcsRequest = new DescribeVpcsRequest(); + describeVpcsRequest.setVpcId(vSwitch.getVpcId()); + DescribeVpcsResponse describeVpcsResponse = client.getAcsResponse(describeVpcsRequest); + Vpc vpc = describeVpcsResponse.getVpcs().get(0); + AliCloudSubnetCachingAgent.InnerVSwitche innerVSwitche = + transformation(vSwitch, vpc.getVpcName()); + Map attributes = objectMapper.convertValue(innerVSwitche, Map.class); + CacheData data = + new DefaultCacheData( + Keys.getSubnetKey(vSwitch.getVSwitchId(), region, account.getName()), + attributes, + new HashMap<>(16)); + datas.add(data); + } + + } catch (ClientException e) { + e.printStackTrace(); + } + + resultMap.put(SUBNETS.ns, datas); + + return new DefaultCacheResult(resultMap); + } + + private InnerVSwitche transformation(VSwitch vSwitch, String vpcName) { + InnerVSwitche innerVSwitche = new InnerVSwitche(); + innerVSwitche.setAccount(account.getName()); + innerVSwitche.setRegion(region); + innerVSwitche.setStatus(vSwitch.getStatus()); + innerVSwitche.setVpcId(vSwitch.getVpcId()); + innerVSwitche.setVSwitchId(vSwitch.getVSwitchId()); + innerVSwitche.setVSwitchName(vSwitch.getVSwitchName()); + innerVSwitche.setZoneId(vSwitch.getZoneId()); + innerVSwitche.setVpcName(vpcName); + return innerVSwitche; + } + + @Data + class InnerVSwitche { + private String account; + private String region; + private String status; + private String vSwitchId; + private String vSwitchName; + private String vpcId; + private String zoneId; + private String type = AliCloudProvider.ID; + private String vpcName; + } + + @Override + public Collection getProvidedDataTypes() { + return types; + } + + @Override + public String getAgentType() { + return account.getName() + "/" + region + "/" + this.getClass().getSimpleName(); + } + + @Override + public String getProviderName() { + return AliProvider.PROVIDER_NAME; + } + + @Override + public String getAccountName() { + return account.getName(); + } +} diff --git a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/provider/view/AliCloudApplicationProvider.java b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/provider/view/AliCloudApplicationProvider.java new file mode 100644 index 00000000000..228f7f64c1d --- /dev/null +++ b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/provider/view/AliCloudApplicationProvider.java @@ -0,0 +1,101 @@ +/* + * Copyright 2022 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.clouddriver.alicloud.provider.view; + +import static com.netflix.spinnaker.clouddriver.core.provider.agent.Namespace.APPLICATIONS; +import static com.netflix.spinnaker.clouddriver.core.provider.agent.Namespace.CLUSTERS; + +import com.netflix.spinnaker.cats.cache.Cache; +import com.netflix.spinnaker.cats.cache.CacheData; +import com.netflix.spinnaker.clouddriver.alicloud.AliCloudProvider; +import com.netflix.spinnaker.clouddriver.alicloud.cache.Keys; +import com.netflix.spinnaker.clouddriver.alicloud.model.AliCloudApplication; +import com.netflix.spinnaker.clouddriver.model.Application; +import com.netflix.spinnaker.clouddriver.model.ApplicationProvider; +import java.util.Collection; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Objects; +import java.util.Set; +import java.util.stream.Collectors; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Component +public class AliCloudApplicationProvider implements ApplicationProvider { + @Autowired private Cache cacheView; + @Autowired private AliCloudProvider provider; + + @Override + public Set getApplications(boolean expand) { + Collection cacheDatas = + cacheView.getAll(APPLICATIONS.ns, Keys.getApplicationKey("*")); + if (cacheDatas == null) { + return null; + } + + return cacheDatas.stream() + .filter(Objects::nonNull) + .map( + cacheData -> { + String name = String.valueOf(cacheData.getAttributes().get("name")); + return buildAliCloudApplication(name, cacheData, expand); + }) + .collect(Collectors.toSet()); + } + + @Nullable + @Override + public Application getApplication(String name) { + CacheData application = cacheView.get(APPLICATIONS.ns, Keys.getApplicationKey(name)); + if (application == null) { + return null; + } + return buildAliCloudApplication(name, application, true); + } + + @NotNull + private AliCloudApplication buildAliCloudApplication( + String name, CacheData application, boolean expand) { + Map> clusterNames = new HashMap<>(); + if (!expand) { + return new AliCloudApplication(name, clusterNames, Map.of("name", name)); + } + + Collection clusterKeys = application.getRelationships().get(CLUSTERS.ns); + for (String key : clusterKeys) { + Map clusterinfo = Keys.parse(key); + if (clusterinfo == null) { + continue; + } + clusterNames.compute( + clusterinfo.get("account"), + (k, v) -> { + if (v == null) { + v = new HashSet<>(); + } + v.add(clusterinfo.get("clusterName")); + return v; + }); + } + + return new AliCloudApplication(name, clusterNames, Map.of("name", name)); + } +} diff --git a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/provider/view/AliCloudClusterProvider.java b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/provider/view/AliCloudClusterProvider.java new file mode 100644 index 00000000000..ac8cc6a47b4 --- /dev/null +++ b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/provider/view/AliCloudClusterProvider.java @@ -0,0 +1,369 @@ +/* + * Copyright 2022 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.clouddriver.alicloud.provider.view; + +import static com.netflix.spinnaker.clouddriver.core.provider.agent.Namespace.*; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.netflix.spinnaker.cats.cache.Cache; +import com.netflix.spinnaker.cats.cache.CacheData; +import com.netflix.spinnaker.clouddriver.alicloud.AliCloudProvider; +import com.netflix.spinnaker.clouddriver.alicloud.cache.Keys; +import com.netflix.spinnaker.clouddriver.alicloud.common.DateParseHelper; +import com.netflix.spinnaker.clouddriver.alicloud.common.HealthHelper; +import com.netflix.spinnaker.clouddriver.alicloud.common.Sets; +import com.netflix.spinnaker.clouddriver.alicloud.model.AliCloudCluster; +import com.netflix.spinnaker.clouddriver.alicloud.model.AliCloudInstance; +import com.netflix.spinnaker.clouddriver.alicloud.model.AliCloudLoadBalancer; +import com.netflix.spinnaker.clouddriver.alicloud.model.AliCloudServerGroup; +import com.netflix.spinnaker.clouddriver.alicloud.provider.agent.AliCloudLoadBalancerCachingAgent.LoadBalancerCacheBuilder; +import com.netflix.spinnaker.clouddriver.model.ClusterProvider; +import com.netflix.spinnaker.clouddriver.model.HealthState; +import com.netflix.spinnaker.clouddriver.model.ServerGroup; +import com.netflix.spinnaker.clouddriver.model.ServerGroup.Capacity; +import com.netflix.spinnaker.clouddriver.model.ServerGroupProvider; +import java.text.ParseException; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.Date; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; +import java.util.Set; +import java.util.stream.Collectors; +import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Component +public class AliCloudClusterProvider + implements ClusterProvider, ServerGroupProvider { + + private final ObjectMapper objectMapper; + + private final Cache cacheView; + + private final AliCloudProvider provider; + + @Autowired + public AliCloudClusterProvider( + ObjectMapper objectMapper, Cache cacheView, AliCloudProvider provider) { + this.objectMapper = objectMapper; + this.cacheView = cacheView; + this.provider = provider; + } + + @Override + public Map> getClusterDetails(String applicationName) { + CacheData application = cacheView.get(APPLICATIONS.ns, Keys.getApplicationKey(applicationName)); + if (application == null) { + return Collections.emptyMap(); + } + Collection clusterKeys = application.getRelationships().get(CLUSTERS.ns); + Collection clusterCaches = cacheView.getAll(CLUSTERS.ns, clusterKeys); + + List aliCloudClusters = + clusterCaches.stream() + .map(clusterCache -> translateClusters(clusterCache, true)) + .collect(Collectors.toList()); + + Map> resultMap = new HashMap<>(16); + resultMap.put(applicationName, new HashSet<>(aliCloudClusters)); + return resultMap; + } + + @Override + public AliCloudCluster getCluster( + String application, String account, String name, boolean includeDetails) { + String clusterKey = Keys.getClusterKey(name, application, account); + CacheData cluster = cacheView.get(CLUSTERS.ns, clusterKey); + if (cluster == null) { + return null; + } else { + return translateClusters(cluster, includeDetails); + } + } + + private AliCloudCluster translateClusters(CacheData clusterData, boolean includeDetails) { + if (clusterData == null) { + return null; + } + Set serverGroups = new HashSet<>(); + Set loadBalancers = new HashSet<>(); + + String application = (String) clusterData.getAttributes().get("application"); + String accountName = (String) clusterData.getAttributes().get("accountName"); + if (!includeDetails) { + return new AliCloudCluster(application, accountName, serverGroups, loadBalancers); + } + + Map> relationships = clusterData.getRelationships(); + Collection serverGroupKeys = relationships.get(SERVER_GROUPS.ns); + Collection serverGroupCaches = cacheView.getAll(SERVER_GROUPS.ns, serverGroupKeys); + if (serverGroupCaches != null) { + serverGroups = + serverGroupCaches.stream().map(this::buildServerGroup).collect(Collectors.toSet()); + } + + Collection loadBalancerKeys = relationships.get(LOAD_BALANCERS.ns); + if (loadBalancerKeys != null && loadBalancerKeys.size() > 0) { + Collection loadBalancerCacheData = + cacheView.getAll(LOAD_BALANCERS.ns, loadBalancerKeys.toArray(new String[] {})); + loadBalancers = + loadBalancerCacheData.stream() + .map(cacheData -> LoadBalancerCacheBuilder.buildLoadBalancer(cacheData, objectMapper)) + .collect(Collectors.toSet()); + } + + return new AliCloudCluster(application, accountName, serverGroups, loadBalancers); + } + + @Override + public AliCloudServerGroup getServerGroup( + String account, String region, String name, boolean includeDetails) { + String serverGroupKey = Keys.getServerGroupKey(name, account, region); + CacheData serverGroupData = cacheView.get(SERVER_GROUPS.ns, serverGroupKey); + + if (serverGroupData == null) { + return null; + } + + return buildServerGroup(serverGroupData); + } + + private AliCloudServerGroup buildServerGroup(CacheData serverGroupCache) { + Map attributes = serverGroupCache.getAttributes(); + + Collection allHealthyKeys = cacheView.getIdentifiers(HEALTH.ns); + + AliCloudServerGroup serverGroup = new AliCloudServerGroup(); + + serverGroup.setType(AliCloudProvider.ID); + serverGroup.setName(String.valueOf(attributes.get("name"))); + serverGroup.setCloudProvider(AliCloudProvider.ID); + serverGroup.setRegion(String.valueOf(attributes.get("region"))); + + Map scalingGroup = (Map) attributes.get("scalingGroup"); + serverGroup.setResult(scalingGroup); + + String scalingGroupLifecycleState = (String) scalingGroup.get("lifecycleState"); + serverGroup.setDisabled(!"Active".equals(scalingGroupLifecycleState)); + + String creationTime = String.valueOf(scalingGroup.get("creationTime")); + serverGroup.setCreationTime(creationTime); + try { + Date d = DateParseHelper.parseUTCTime(creationTime); + serverGroup.setCreatedTime(d.getTime()); + } catch (ParseException e) { + e.printStackTrace(); + } + List instances = (List) attributes.get("instances"); + Set loadBalancerIds = new HashSet<>(); + if (scalingGroup.containsKey("loadBalancerIds")) { + loadBalancerIds.addAll((List) scalingGroup.get("loadBalancerIds")); + ArrayList vServerGroups = (ArrayList) scalingGroup.get("vserverGroups"); + if (vServerGroups != null) { + vServerGroups.forEach( + vServerGroup -> { + loadBalancerIds.add(String.valueOf(vServerGroup.get("loadBalancerId"))); + }); + } + } + serverGroup.setLoadBalancers(loadBalancerIds); + + serverGroup.setInstances( + instances.stream() + .map( + instance -> + buildAliCloudInstance( + allHealthyKeys, scalingGroupLifecycleState, loadBalancerIds, instance)) + .filter(Objects::nonNull) + .collect(Collectors.toSet())); + + // build capacity + Capacity capacity = new Capacity(); + Object maxSize = scalingGroup.get("maxSize"); + Object minSize = scalingGroup.get("minSize"); + + capacity.setMax((Integer) maxSize); + capacity.setMin((Integer) minSize); + if (instances.size() >= (Integer) minSize) { + capacity.setDesired(instances.size()); + } else { + capacity.setDesired((Integer) minSize); + } + serverGroup.setCapacity(capacity); + serverGroup.setInstanceCounts(buildInstanceCounts(serverGroup)); + serverGroup.setResult(serverGroupCache.getAttributes()); + + // build image info + Map scalingConfiguration = (Map) attributes.get("scalingConfiguration"); + serverGroup.setLaunchConfig(scalingConfiguration); + Map image = new HashMap<>(); + image.put("name", scalingConfiguration.get("imageId")); + image.put("imageId", scalingConfiguration.get("imageId")); + Map buildInfo = new HashMap(); + buildInfo.put("imageId", scalingConfiguration.get("imageId")); + serverGroup.setImage(image); + serverGroup.setBuildInfo(buildInfo); + serverGroup.setSecurityGroups( + Sets.ofModifiable(String.valueOf(scalingConfiguration.get("securityGroupId")))); + + return serverGroup; + } + + private AliCloudInstance buildAliCloudInstance( + Collection allHealthyKeys, + String scalingGroupLifecycleState, + Set loadBalancerIds, + Map instance) { + String instanceId = String.valueOf(instance.get("instanceId")); + if (StringUtils.isBlank(instanceId)) { + return null; + } + + String zone = (String) instance.get("creationType"); + + List healthKeys = + allHealthyKeys.stream() + .filter(k -> HealthHelper.healthyStateMatcher(k, loadBalancerIds, instanceId)) + .collect(Collectors.toList()); + Collection healthDatas = + cacheView.getAll(HEALTH.ns, healthKeys.toArray(new String[] {})); + HealthState healthState = + HealthHelper.genInstanceHealthState( + scalingGroupLifecycleState, String.valueOf(instance.get("healthStatus")), healthDatas); + + List> health = new ArrayList<>(); + Map m = new HashMap<>(); + m.put("type", provider.getDisplayName()); + m.put("healthClass", "platform"); + m.put("state", healthState); + health.add(m); + + return new AliCloudInstance(instanceId, null, zone, healthState, health); + } + + private ServerGroup.InstanceCounts buildInstanceCounts(AliCloudServerGroup serverGroup) { + Map countMap = new HashMap<>(16); + ServerGroup.InstanceCounts instanceCounts = new ServerGroup.InstanceCounts(); + instanceCounts.setTotal(serverGroup.getInstances().size()); + serverGroup + .getInstances() + .forEach( + instance -> { + if (instance.getHealthState().equals(HealthState.Up)) { + countMap.put("up", countMap.getOrDefault("up", 0) + 1); + } + if (instance.getHealthState().equals(HealthState.Down)) { + countMap.put("down", countMap.getOrDefault("down", 0) + 1); + } + if (instance.getHealthState().equals(HealthState.Unknown)) { + countMap.put("unKnown", countMap.getOrDefault("unKnown", 0) + 1); + } + }); + instanceCounts.setUp(countMap.getOrDefault("up", 0)); + instanceCounts.setDown(countMap.getOrDefault("down", 0)); + instanceCounts.setUnknown(countMap.getOrDefault("unKnown", 0)); + return instanceCounts; + } + + @Override + public ServerGroup getServerGroup(String account, String region, String name) { + String serverGroupKey = Keys.getServerGroupKey(name, account, region); + CacheData serverGroupData = cacheView.get(SERVER_GROUPS.ns, serverGroupKey); + if (serverGroupData == null) { + return null; + } + return buildServerGroup(serverGroupData); + } + + @Override + public String getCloudProviderId() { + return AliCloudProvider.ID; + } + + @Override + public boolean supportsMinimalClusters() { + return false; + } + + @Override + public Collection getServerGroupIdentifiers(String account, String region) { + account = Optional.ofNullable(account).orElse("*"); + region = Optional.ofNullable(region).orElse("*"); + return cacheView.filterIdentifiers( + SERVER_GROUPS.ns, Keys.getServerGroupKey("*", "*", account, region)); + } + + @Override + public String buildServerGroupIdentifier(String account, String region, String serverGroupName) { + return Keys.getServerGroupKey(serverGroupName, account, region); + } + + @Override + public Map> getClusters() { + Collection clusterData = cacheView.getAll(CLUSTERS.ns); + Collection clusters = + clusterData.stream().map(d -> translateClusters(d, false)).collect(Collectors.toList()); + + return mapResponse(clusters); + } + + @Override + public Map> getClusterSummaries(String application) { + return getClusterDetails(application); + } + + @Override + public Set getClusters(String application, String account) { + Map> clusterDetails = getClusterDetails(application); + return clusterDetails.isEmpty() ? Collections.emptySet() : clusterDetails.get(account); + } + + @Override + public Set getClusters( + String application, String account, boolean includeDetails) { + Map> clusterDetails = getClusterDetails(application); + return clusterDetails.isEmpty() ? Collections.emptySet() : clusterDetails.get(account); + } + + @Override + public AliCloudCluster getCluster(String application, String account, String name) { + CacheData cluster = cacheView.get(CLUSTERS.ns, Keys.getClusterKey(name, application, account)); + + return translateClusters(cluster, true); + } + + private static Map> mapResponse( + Collection clusters) { + Map> results = + clusters.stream() + .collect(Collectors.groupingBy(AliCloudCluster::getAccountName, Collectors.toSet())); + // Map> results = + // clusters.stream().collect(Collectors.toMap(AliCloudCluster::getAccountName, p -> + // Collections.singleton(p), (x, y) ->{ + // Set set = new HashSet<>(x); + // set.addAll(y); + // return set; + // })); + return results; + } +} diff --git a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/provider/view/AliCloudInstanceProvider.java b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/provider/view/AliCloudInstanceProvider.java new file mode 100644 index 00000000000..6498a3c4236 --- /dev/null +++ b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/provider/view/AliCloudInstanceProvider.java @@ -0,0 +1,102 @@ +/* + * Copyright 2022 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.clouddriver.alicloud.provider.view; + +import static com.netflix.spinnaker.clouddriver.core.provider.agent.Namespace.HEALTH; +import static com.netflix.spinnaker.clouddriver.core.provider.agent.Namespace.INSTANCES; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.netflix.spinnaker.cats.cache.Cache; +import com.netflix.spinnaker.cats.cache.CacheData; +import com.netflix.spinnaker.clouddriver.alicloud.AliCloudProvider; +import com.netflix.spinnaker.clouddriver.alicloud.cache.Keys; +import com.netflix.spinnaker.clouddriver.alicloud.common.HealthHelper; +import com.netflix.spinnaker.clouddriver.alicloud.model.AliCloudInstance; +import com.netflix.spinnaker.clouddriver.model.HealthState; +import com.netflix.spinnaker.clouddriver.model.InstanceProvider; +import java.util.*; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Component +public class AliCloudInstanceProvider implements InstanceProvider { + + private final ObjectMapper objectMapper; + private final Cache cacheView; + private final AliCloudProvider provider; + + @Autowired + public AliCloudInstanceProvider( + ObjectMapper objectMapper, Cache cacheView, AliCloudProvider provider) { + this.objectMapper = objectMapper; + this.cacheView = cacheView; + this.provider = provider; + } + + @Override + public AliCloudInstance getInstance(String account, String region, String id) { + + CacheData instanceEntry = cacheView.get(INSTANCES.ns, Keys.getInstanceKey(id, account, region)); + if (instanceEntry == null) { + return null; + } + + Map attributes = instanceEntry.getAttributes(); + String instanceId = (String) attributes.get("instanceId"); + String zone = (String) attributes.get("zoneId"); + + if (instanceId != null) { + Collection cacheKeys = + cacheView.filterIdentifiers( + HEALTH.ns, Keys.getInstanceHealthKey("*", id, "*", account, region, "*")); + Collection healthData = + cacheView.getAll(HEALTH.ns, cacheKeys.toArray(new String[] {})); + + HealthState healthState = + HealthHelper.genInstanceHealthState(String.valueOf(attributes.get("status")), healthData); + + List> health = new ArrayList<>(); + Map m = new HashMap<>(); + m.put("type", provider.getDisplayName()); + m.put("healthClass", "platform"); + m.put("state", healthState); + health.add(m); + + AliCloudInstance instance = + new AliCloudInstance(String.valueOf(id), null, zone, healthState, health); + instance.setAttributes(attributes); + + return instance; + } + return null; + } + + @Override + public String getConsoleOutput(String account, String region, String id) { + CacheData instanceEntry = cacheView.get(INSTANCES.ns, Keys.getInstanceKey(id, account, region)); + if (instanceEntry == null) { + return null; + } + + Map attributes = instanceEntry.getAttributes(); + return String.valueOf(attributes.getOrDefault("instanceName", "")); + } + + @Override + public String getCloudProvider() { + return AliCloudProvider.ID; + } +} diff --git a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/provider/view/AliCloudInstanceTypeProvider.java b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/provider/view/AliCloudInstanceTypeProvider.java new file mode 100644 index 00000000000..56cbcf6a37e --- /dev/null +++ b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/provider/view/AliCloudInstanceTypeProvider.java @@ -0,0 +1,101 @@ +/* + * Copyright 2022 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.clouddriver.alicloud.provider.view; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.netflix.spinnaker.cats.cache.Cache; +import com.netflix.spinnaker.cats.cache.CacheData; +import com.netflix.spinnaker.cats.cache.RelationshipCacheFilter; +import com.netflix.spinnaker.clouddriver.alicloud.cache.Keys; +import com.netflix.spinnaker.clouddriver.alicloud.provider.view.AliCloudInstanceTypeProvider.Zone; +import com.netflix.spinnaker.clouddriver.model.InstanceType; +import com.netflix.spinnaker.clouddriver.model.InstanceTypeProvider; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import lombok.Data; +import lombok.EqualsAndHashCode; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Component +public class AliCloudInstanceTypeProvider implements InstanceTypeProvider { + + private final ObjectMapper objectMapper; + + private final Cache cacheView; + + @Autowired + public AliCloudInstanceTypeProvider(ObjectMapper objectMapper, Cache cacheView) { + this.objectMapper = objectMapper; + this.cacheView = cacheView; + } + + @Override + public Set getAll() { + Set results = new HashSet<>(); + String globalKey = Keys.getInstanceTypeKey("*", "*", "*"); + Collection allInstanceTypeKeys = + cacheView.filterIdentifiers(Keys.Namespace.INSTANCE_TYPES.ns, globalKey); + Collection allData = + cacheView.getAll( + Keys.Namespace.INSTANCE_TYPES.ns, allInstanceTypeKeys, RelationshipCacheFilter.none()); + for (CacheData allDatum : allData) { + Map attributes = allDatum.getAttributes(); + + Zone zone = + new Zone( + String.valueOf(attributes.get("account")), + String.valueOf(attributes.get("regionId")), + String.valueOf(attributes.get("zoneId")), + String.valueOf(attributes.get("provider")), + objectMapper.convertValue(attributes.get("names"), ArrayList.class)); + results.add(zone); + } + return results; + } + + @Data + @EqualsAndHashCode + class Zone implements InstanceType { + String account; + String regionId; + String zoneId; + String provider; + List instanceTypes; + + public Zone( + String account, + String regionId, + String zoneId, + String provider, + List instanceTypes) { + this.account = account; + this.regionId = regionId; + this.zoneId = zoneId; + this.provider = provider; + this.instanceTypes = instanceTypes; + } + + @Override + public String getName() { + return account; + } + } +} diff --git a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/provider/view/AliCloudKeyPairProvider.java b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/provider/view/AliCloudKeyPairProvider.java new file mode 100644 index 00000000000..ad0c46eed3f --- /dev/null +++ b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/provider/view/AliCloudKeyPairProvider.java @@ -0,0 +1,70 @@ +/* + * Copyright 2022 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.clouddriver.alicloud.provider.view; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.netflix.spinnaker.cats.cache.Cache; +import com.netflix.spinnaker.cats.cache.CacheData; +import com.netflix.spinnaker.cats.cache.RelationshipCacheFilter; +import com.netflix.spinnaker.clouddriver.alicloud.cache.Keys; +import com.netflix.spinnaker.clouddriver.alicloud.cache.Keys.Namespace; +import com.netflix.spinnaker.clouddriver.alicloud.model.AliCloudKeyPair; +import com.netflix.spinnaker.clouddriver.model.KeyPairProvider; +import java.util.Collection; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Component +public class AliCloudKeyPairProvider implements KeyPairProvider { + + private final ObjectMapper objectMapper; + + private final Cache cacheView; + + @Autowired + public AliCloudKeyPairProvider(ObjectMapper objectMapper, Cache cacheView) { + this.objectMapper = objectMapper; + this.cacheView = cacheView; + } + + @Override + public Set getAll() { + + Set results = new HashSet<>(); + + String key = Keys.getKeyPairKey("*", "*", "*"); + Collection allKeyPairKeys = + cacheView.filterIdentifiers(Namespace.ALI_CLOUD_KEY_PAIRS.ns, key); + Collection allData = + cacheView.getAll( + Namespace.ALI_CLOUD_KEY_PAIRS.ns, allKeyPairKeys, RelationshipCacheFilter.none()); + for (CacheData allDatum : allData) { + Map attributes = allDatum.getAttributes(); + AliCloudKeyPair keyPair = + new AliCloudKeyPair( + String.valueOf(attributes.get("account")), + String.valueOf(attributes.get("regionId")), + String.valueOf(attributes.get("keyPairName")), + String.valueOf(attributes.get("keyPairFingerPrint"))); + results.add(keyPair); + } + + return results; + } +} diff --git a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/provider/view/AliCloudLoadBalancerProvider.java b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/provider/view/AliCloudLoadBalancerProvider.java index 75d57597f9d..bbcc2083f28 100644 --- a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/provider/view/AliCloudLoadBalancerProvider.java +++ b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/provider/view/AliCloudLoadBalancerProvider.java @@ -20,17 +20,22 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.netflix.spinnaker.cats.cache.Cache; import com.netflix.spinnaker.cats.cache.CacheData; -import com.netflix.spinnaker.cats.cache.CacheFilter; -import com.netflix.spinnaker.cats.cache.RelationshipCacheFilter; import com.netflix.spinnaker.clouddriver.alicloud.AliCloudProvider; import com.netflix.spinnaker.clouddriver.alicloud.cache.Keys; import com.netflix.spinnaker.clouddriver.alicloud.common.HealthHelper; +import com.netflix.spinnaker.clouddriver.alicloud.common.Sets; import com.netflix.spinnaker.clouddriver.alicloud.model.AliCloudLoadBalancer; -import com.netflix.spinnaker.clouddriver.model.HealthState; import com.netflix.spinnaker.clouddriver.model.LoadBalancerInstance; import com.netflix.spinnaker.clouddriver.model.LoadBalancerProvider; import com.netflix.spinnaker.clouddriver.model.LoadBalancerServerGroup; -import java.util.*; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; import java.util.regex.Pattern; import java.util.stream.Collectors; import org.springframework.beans.factory.annotation.Autowired; @@ -53,24 +58,20 @@ public AliCloudLoadBalancerProvider( this.provider = provider; } - private static final String SURVIVE_STATUS = "Active"; - @Override public Set getApplicationLoadBalancers(String applicationName) { - Set loadBalancerKeys = new HashSet<>(); - Set loadBalances = new HashSet<>(); Collection applicationServerGroups = getServerGroupCacheDataByApplication(applicationName); - Collection allHealthyKeys = cacheView.getIdentifiers(HEALTH.ns); Collection allLoadBalancerKeys = cacheView.getIdentifiers(LOAD_BALANCERS.ns); Collection loadBalancerKeyMatches = allLoadBalancerKeys.stream() .filter(tab -> applicationMatcher(tab, applicationName)) .collect(Collectors.toList()); - loadBalancerKeys.addAll(loadBalancerKeyMatches); Collection loadBalancerData = - cacheView.getAll(LOAD_BALANCERS.ns, loadBalancerKeys, null); + cacheView.getAll(LOAD_BALANCERS.ns, loadBalancerKeyMatches); + + Set loadBalances = new HashSet<>(); for (CacheData cacheData : loadBalancerData) { Map attributes = objectMapper.convertValue(cacheData.getAttributes(), Map.class); @@ -87,7 +88,17 @@ public Set getApplicationLoadBalancers(String applicationN applicationServerGroup.getRelationships().get("loadBalancers"); for (String balancer : loadBalancers) { if (id.startsWith(balancer)) { - addServerGroupToLoadBalancer(allHealthyKeys, loadBalancer, applicationServerGroup); + addServerGroupToLoadBalancer(loadBalancer, applicationServerGroup); + + Map map = + (Map) + applicationServerGroup.getAttributes().get("scalingConfiguration"); + if (map.containsKey("securityGroupIds")) { + Set securityGroups = new HashSet<>(); + securityGroups.addAll((Collection) map.get("securityGroupIds")); + loadBalancer.setSecurityGroups(securityGroups); + } + break; } } @@ -105,7 +116,6 @@ public List byAccountAndRegionAndName(String account, String regi cacheView.filterIdentifiers(LOAD_BALANCERS.ns, searchKey); Collection loadBalancers = cacheView.getAll(LOAD_BALANCERS.ns, allLoadBalancerKeys, null); - Collection allHealthyKeys = cacheView.getIdentifiers(HEALTH.ns); for (CacheData loadBalancer : loadBalancers) { ResultDetails resultDetails = new ResultDetails(); Set serverGroups = new HashSet<>(); @@ -119,8 +129,7 @@ public List byAccountAndRegionAndName(String account, String regi for (String loadBalancerId : relationships) { if (id.startsWith(loadBalancerId)) { LoadBalancerServerGroup loadBalancerServerGroup = - createLoadBalancerServerGroup( - allHealthyKeys, loadBalancerId, applicationServerGroup); + createLoadBalancerServerGroup(loadBalancerId, applicationServerGroup); serverGroups.add(loadBalancerServerGroup); break; } @@ -158,19 +167,8 @@ private static boolean applicationMatcher(String key, String applicationName) { || Pattern.matches(regex3, key); } - Collection resolveRelationshipData( - CacheData source, String relationship, CacheFilter cacheFilter) { - Map> relationships = source.getRelationships(); - Collection keys = relationships.get(relationship); - if (!keys.isEmpty()) { - return cacheView.getAll(relationship, keys, null); - } else { - return new ArrayList(); - } - } - private LoadBalancerServerGroup createLoadBalancerServerGroup( - Collection allHealthyKeys, String loadBalancerId, CacheData applicationServerGroup) { + String loadBalancerId, CacheData applicationServerGroup) { LoadBalancerServerGroup loadBalancerServerGroup = new LoadBalancerServerGroup(); Map attributes = applicationServerGroup.getAttributes(); loadBalancerServerGroup.setName(String.valueOf(attributes.get("name"))); @@ -178,70 +176,81 @@ private LoadBalancerServerGroup createLoadBalancerServerGroup( loadBalancerServerGroup.setRegion(String.valueOf(attributes.get("region"))); loadBalancerServerGroup.setAccount(String.valueOf(attributes.get("account"))); Map scalingGroup = (Map) attributes.get("scalingGroup"); - String lifecycleState = (String) scalingGroup.get("lifecycleState"); - if (SURVIVE_STATUS.equals(lifecycleState)) { - loadBalancerServerGroup.setIsDisabled(false); - } else { - loadBalancerServerGroup.setIsDisabled(true); - } - Set detachedInstances = new HashSet<>(); - Set loadBalancerInstances = new HashSet<>(); + String scalingGroupLifecycleState = (String) scalingGroup.get("lifecycleState"); + loadBalancerServerGroup.setIsDisabled(!"Active".equals(scalingGroupLifecycleState)); + List instances = (List) attributes.get("instances"); - for (Map instance : instances) { - Object id = instance.get("instanceId"); - if (id != null) { - String instanceId = String.valueOf(id); - String healthStatus = (String) instance.get("healthStatus"); - boolean flag = "Healthy".equals(healthStatus); - Map health = new HashMap<>(); - health.put("type", provider.getDisplayName()); - health.put("healthClass", "platform"); - List loadBalancerIds = - new ArrayList() { - { - add(loadBalancerId); - } - }; - HealthState healthState = - HealthHelper.judgeInstanceHealthyState( - allHealthyKeys, loadBalancerIds, instanceId, cacheView); - health.put( - "state", - !"Active".equals(lifecycleState) - ? "unhealthy" - : !flag - ? "unhealthy" - : healthState.equals(HealthState.Up) - ? "healthy" - : healthState.equals(HealthState.Unknown) ? "unknown" : "unhealthy"); - String zone = (String) instance.get("creationType"); - LoadBalancerInstance loadBalancerInstance = new LoadBalancerInstance(); - loadBalancerInstance.setId(instanceId); - loadBalancerInstance.setName(instanceId); - loadBalancerInstance.setZone(zone); - loadBalancerInstance.setHealth(health); - loadBalancerInstances.add(loadBalancerInstance); - detachedInstances.add(instanceId); - } + if (instances != null) { + Collection allHealthyKeys = cacheView.getIdentifiers(HEALTH.ns); + + loadBalancerServerGroup.setInstances( + instances.stream() + .filter(instance -> instance.get("instanceId") != null) + .map( + instance -> { + LoadBalancerInstance loadBalancerInstance = new LoadBalancerInstance(); + String instanceId = String.valueOf(instance.get("instanceId")); + loadBalancerInstance.setId(instanceId); + loadBalancerInstance.setName(instanceId); + loadBalancerInstance.setZone(String.valueOf(instance.get("creationType"))); + + List healthKeys = + allHealthyKeys.stream() + .filter( + k -> + HealthHelper.healthyStateMatcher( + k, Sets.ofModifiable(loadBalancerId), instanceId)) + .collect(Collectors.toList()); + Collection healthDatas = + cacheView.getAll(HEALTH.ns, healthKeys.toArray(new String[] {})); + + Map health = new HashMap<>(); + health.put("type", provider.getDisplayName()); + health.put("healthClass", "platform"); + health.put( + "state", + HealthHelper.loadBalancerInstanceHealthState( + scalingGroupLifecycleState, + String.valueOf(instance.get("healthStatus")), + healthDatas)); + loadBalancerInstance.setHealth(health); + return loadBalancerInstance; + }) + .collect(Collectors.toSet())); } - // loadBalancerServerGroup.setDetachedInstances(detachedInstances); - loadBalancerServerGroup.setInstances(loadBalancerInstances); + return loadBalancerServerGroup; } - private void addServerGroupToLoadBalancer( - Collection allHealthyKeys, - AliCloudLoadBalancer loadBalancer, - CacheData applicationServerGroup) { - Set serverGroups = - loadBalancer.getServerGroups() != null ? loadBalancer.getServerGroups() : new HashSet<>(); + public void addServerGroupToLoadBalancer( + AliCloudLoadBalancer loadBalancer, CacheData applicationServerGroup) { + if (!isContainsLoadBalancer(loadBalancer, applicationServerGroup)) { + return; + } + Set serverGroups = loadBalancer.getServerGroups(); + if (serverGroups == null) { + serverGroups = new HashSet<>(); + } LoadBalancerServerGroup serverGroup = - createLoadBalancerServerGroup( - allHealthyKeys, loadBalancer.getLoadBalancerId(), applicationServerGroup); + createLoadBalancerServerGroup(loadBalancer.getLoadBalancerId(), applicationServerGroup); serverGroups.add(serverGroup); loadBalancer.setServerGroups(serverGroups); } + private boolean isContainsLoadBalancer( + AliCloudLoadBalancer loadBalancer, CacheData applicationServerGroup) { + String loadBalancerKey = + Keys.getLoadBalancerKey( + loadBalancer.getName(), + loadBalancer.getAccount(), + loadBalancer.getRegion(), + loadBalancer.getVpcId()); + return applicationServerGroup + .getRelationships() + .get(LOAD_BALANCERS.ns) + .contains(loadBalancerKey); + } + class ResultDetails implements Details { Map results; @@ -261,14 +270,12 @@ private String getApplicationByName(String name) { private Collection getServerGroupCacheDataByApplication(String applicationName) { CacheData application = cacheView.get(APPLICATIONS.ns, Keys.getApplicationKey(applicationName)); - Collection applicationServerGroups = new ArrayList<>(); - if (application != null) { - applicationServerGroups = - resolveRelationshipData( - application, - SERVER_GROUPS.ns, - RelationshipCacheFilter.include(INSTANCES.ns, LOAD_BALANCERS.ns)); + if (application == null) { + return Collections.emptyList(); } - return applicationServerGroups; + Map> relationships = application.getRelationships(); + Collection keys = relationships.get(SERVER_GROUPS.ns); + + return cacheView.getAll(SERVER_GROUPS.ns, keys); } } diff --git a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/provider/view/AliCloudSecurityGroupProvider.java b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/provider/view/AliCloudSecurityGroupProvider.java index 401969812bb..ae683996832 100644 --- a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/provider/view/AliCloudSecurityGroupProvider.java +++ b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/provider/view/AliCloudSecurityGroupProvider.java @@ -27,7 +27,14 @@ import com.netflix.spinnaker.clouddriver.model.SecurityGroupProvider; import com.netflix.spinnaker.clouddriver.model.securitygroups.Rule; import com.netflix.spinnaker.clouddriver.model.securitygroups.Rule.PortRange; -import java.util.*; +import java.util.Collection; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.SortedSet; +import java.util.TreeSet; +import org.jetbrains.annotations.Nullable; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @@ -52,16 +59,18 @@ public Collection getAllByAccount(boolean includeRules, S @Override public AliCloudSecurityGroup get(String account, String region, String name, String vpcId) { String key = Keys.getSecurityGroupKey(name, "*", region, account, vpcId); - return getByKey(key); + return filterByKey(key); } @Override public AliCloudSecurityGroup getById(String account, String region, String id, String vpcId) { String key = Keys.getSecurityGroupKey("*", id, region, account, vpcId); - return getByKey(key); + + return filterByKey(key); } - private AliCloudSecurityGroup getByKey(String key) { + @Nullable + private AliCloudSecurityGroup filterByKey(String key) { Collection allSecurityGroupKeys = cacheView.filterIdentifiers(Namespace.SECURITY_GROUPS.ns, key); Collection allData = diff --git a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/provider/view/AliCloudSubnetProvider.java b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/provider/view/AliCloudSubnetProvider.java new file mode 100644 index 00000000000..b27e1a8cd0f --- /dev/null +++ b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/provider/view/AliCloudSubnetProvider.java @@ -0,0 +1,67 @@ +/* + * Copyright 2022 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.clouddriver.alicloud.provider.view; + +import static com.netflix.spinnaker.clouddriver.alicloud.cache.Keys.Namespace.SUBNETS; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.netflix.spinnaker.cats.cache.Cache; +import com.netflix.spinnaker.cats.cache.CacheData; +import com.netflix.spinnaker.cats.cache.RelationshipCacheFilter; +import com.netflix.spinnaker.clouddriver.alicloud.AliCloudProvider; +import com.netflix.spinnaker.clouddriver.alicloud.cache.Keys; +import com.netflix.spinnaker.clouddriver.alicloud.model.AliCloudSubnet; +import com.netflix.spinnaker.clouddriver.model.SubnetProvider; +import java.util.Collection; +import java.util.HashSet; +import java.util.Set; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Component +public class AliCloudSubnetProvider implements SubnetProvider { + + private final ObjectMapper objectMapper; + + private final Cache cacheView; + + @Autowired + public AliCloudSubnetProvider(ObjectMapper objectMapper, Cache cacheView) { + this.objectMapper = objectMapper; + this.cacheView = cacheView; + } + + @Override + public Set getAll() { + Set results = new HashSet<>(); + String globalKey = Keys.getSubnetKey("*", "*", "*"); + Collection allSubnetKeys = cacheView.filterIdentifiers(SUBNETS.ns, globalKey); + Collection allData = + cacheView.getAll(SUBNETS.ns, allSubnetKeys, RelationshipCacheFilter.none()); + for (CacheData data : allData) { + AliCloudSubnet aliCloudSubnet = + objectMapper.convertValue(data.getAttributes(), AliCloudSubnet.class); + results.add(aliCloudSubnet); + } + + return results; + } + + @Override + public String getCloudProvider() { + return AliCloudProvider.ID; + } +} diff --git a/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/BaseTest.java b/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/BaseTest.java new file mode 100644 index 00000000000..b48e47acae3 --- /dev/null +++ b/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/BaseTest.java @@ -0,0 +1,46 @@ +/* + * Copyright 2022 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.clouddriver.alicloud; + +import com.google.common.io.Files; +import com.google.gson.Gson; +import com.netflix.spinnaker.clouddriver.alicloud.provider.view.CommonProvider; +import java.io.File; +import java.nio.charset.Charset; +import java.util.Objects; + +public class BaseTest { + public static final String ACCOUNT = "test-account"; + public static final String REGION = "cn-test"; + + public static String getFileContentToString(String filePath) { + + String realPath = + Objects.requireNonNull(CommonProvider.class.getClassLoader().getResource(filePath)) + .getPath(); + try { + return Files.toString(new File(realPath), Charset.forName("utf-8")); + } catch (Exception e) { + e.printStackTrace(); + } + return ""; + } + + public T fromMockData(String path, Class classOfT) { + return new Gson().fromJson(getFileContentToString(path), classOfT); + } +} diff --git a/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/provider/agent/AliCloudClusterCachingAgentTest.java b/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/provider/agent/AliCloudClusterCachingAgentTest.java new file mode 100644 index 00000000000..f62e8baa279 --- /dev/null +++ b/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/provider/agent/AliCloudClusterCachingAgentTest.java @@ -0,0 +1,177 @@ +/* + * Copyright 2022 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.clouddriver.alicloud.provider.agent; + +import static com.netflix.spinnaker.clouddriver.core.provider.agent.Namespace.APPLICATIONS; +import static com.netflix.spinnaker.clouddriver.core.provider.agent.Namespace.CLUSTERS; +import static com.netflix.spinnaker.clouddriver.core.provider.agent.Namespace.LAUNCH_CONFIGS; +import static com.netflix.spinnaker.clouddriver.core.provider.agent.Namespace.SERVER_GROUPS; +import static org.junit.Assert.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; + +import com.aliyuncs.AcsResponse; +import com.aliyuncs.ecs.model.v20140526.DescribeInstancesRequest; +import com.aliyuncs.ecs.model.v20140526.DescribeInstancesResponse; +import com.aliyuncs.ecs.model.v20140526.DescribeInstancesResponse.Instance; +import com.aliyuncs.ecs.model.v20140526.DescribeSecurityGroupsRequest; +import com.aliyuncs.ecs.model.v20140526.DescribeSecurityGroupsResponse; +import com.aliyuncs.ecs.model.v20140526.DescribeSecurityGroupsResponse.SecurityGroup; +import com.aliyuncs.ess.model.v20140828.DescribeScalingConfigurationsRequest; +import com.aliyuncs.ess.model.v20140828.DescribeScalingConfigurationsResponse; +import com.aliyuncs.ess.model.v20140828.DescribeScalingConfigurationsResponse.ScalingConfiguration; +import com.aliyuncs.ess.model.v20140828.DescribeScalingGroupsRequest; +import com.aliyuncs.ess.model.v20140828.DescribeScalingGroupsResponse; +import com.aliyuncs.ess.model.v20140828.DescribeScalingGroupsResponse.ScalingGroup; +import com.aliyuncs.ess.model.v20140828.DescribeScalingInstancesRequest; +import com.aliyuncs.ess.model.v20140828.DescribeScalingInstancesResponse; +import com.aliyuncs.ess.model.v20140828.DescribeScalingInstancesResponse.ScalingInstance; +import com.aliyuncs.exceptions.ClientException; +import com.aliyuncs.slb.model.v20140515.DescribeLoadBalancerAttributeRequest; +import com.aliyuncs.slb.model.v20140515.DescribeLoadBalancerAttributeResponse; +import com.netflix.frigga.Names; +import com.netflix.spinnaker.cats.agent.CacheResult; +import com.netflix.spinnaker.cats.cache.CacheData; +import com.netflix.spinnaker.clouddriver.alicloud.cache.Keys; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.Map; +import org.junit.Before; +import org.junit.Test; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; + +public class AliCloudClusterCachingAgentTest extends CommonCachingAgentTest { + + private final String SCALINGGROUPID = "sgid"; + private final String SCALINGGROUPNAME = "spinnaker-ess-test"; + private final String SCALINGCONFIGID = "scid"; + private final String LOADBALANCERID = "lbid"; + private final String LOADBALANCERNAME = "lbname"; + private final String INSTANCEID = "inid"; + private final String INSTANCEID2 = "inid2"; + + @Before + public void testBefore() throws ClientException { + when(client.getAcsResponse(any())).thenAnswer(new CommonAnswer()); + } + + @Test + public void testLoadData() { + + Names name = Names.parseName(SCALINGGROUPNAME); + AliCloudClusterCachingAgent agent = + new AliCloudClusterCachingAgent(account, REGION, objectMapper, client); + + CacheResult result = agent.loadData(providerCache); + + Map> cacheResults = result.getCacheResults(); + Collection applications = cacheResults.get(APPLICATIONS.ns); + Collection clusters = cacheResults.get(CLUSTERS.ns); + Collection serverGroups = cacheResults.get(SERVER_GROUPS.ns); + Collection launchConfigs = cacheResults.get(LAUNCH_CONFIGS.ns); + + String applicationKey = Keys.getApplicationKey(name.getApp()); + String clusterKey = Keys.getClusterKey(name.getCluster(), name.getApp(), ACCOUNT); + String serverGroupKey = Keys.getServerGroupKey(SCALINGGROUPNAME, ACCOUNT, REGION); + String launchConfigKey = Keys.getLaunchConfigKey(SCALINGGROUPNAME, ACCOUNT, REGION); + + assertEquals(1, applications.size()); + assertEquals(applicationKey, applications.iterator().next().getId()); + + assertEquals(1, clusters.size()); + assertEquals(clusterKey, clusters.iterator().next().getId()); + + assertEquals(1, serverGroups.size()); + assertEquals(serverGroupKey, serverGroups.iterator().next().getId()); + + assertEquals(1, launchConfigs.size()); + assertEquals(launchConfigKey, launchConfigs.iterator().next().getId()); + } + + private class CommonAnswer implements Answer { + @Override + public AcsResponse answer(InvocationOnMock invocation) throws Throwable { + Object argument = invocation.getArgument(0); + if (argument instanceof DescribeScalingGroupsRequest) { + DescribeScalingGroupsResponse response = new DescribeScalingGroupsResponse(); + List scalingGroups = new ArrayList<>(); + ScalingGroup scalingGroup = new ScalingGroup(); + scalingGroup.setMinSize(3); + scalingGroup.setMaxSize(10); + scalingGroup.setScalingGroupId(SCALINGGROUPID); + scalingGroup.setActiveScalingConfigurationId(SCALINGCONFIGID); + scalingGroup.setScalingGroupName(SCALINGGROUPNAME); + + List loadBalancerIds = new ArrayList<>(); + loadBalancerIds.add(LOADBALANCERID); + scalingGroup.setLoadBalancerIds(loadBalancerIds); + + scalingGroups.add(scalingGroup); + response.setScalingGroups(scalingGroups); + return response; + } else if (argument instanceof DescribeScalingConfigurationsRequest) { + DescribeScalingConfigurationsResponse response = + new DescribeScalingConfigurationsResponse(); + List scalingConfigurations = new ArrayList<>(); + ScalingConfiguration scalingConfiguration = new ScalingConfiguration(); + scalingConfiguration.setScalingConfigurationId(SCALINGCONFIGID); + + scalingConfigurations.add(scalingConfiguration); + response.setScalingConfigurations(scalingConfigurations); + return response; + } else if (argument instanceof DescribeLoadBalancerAttributeRequest) { + DescribeLoadBalancerAttributeResponse response = + new DescribeLoadBalancerAttributeResponse(); + response.setLoadBalancerId(LOADBALANCERID); + response.setLoadBalancerName(LOADBALANCERNAME); + return response; + } else if (argument instanceof DescribeScalingInstancesRequest) { + DescribeScalingInstancesResponse response = new DescribeScalingInstancesResponse(); + List scalingInstances = new ArrayList<>(); + ScalingInstance instance = new ScalingInstance(); + instance.setInstanceId(INSTANCEID); + + scalingInstances.add(instance); + + ScalingInstance instance2 = new ScalingInstance(); + instance2.setInstanceId(INSTANCEID2); + scalingInstances.add(instance2); + + response.setScalingInstances(scalingInstances); + return response; + } else if (argument instanceof DescribeSecurityGroupsRequest) { + DescribeSecurityGroupsResponse response = new DescribeSecurityGroupsResponse(); + List securityGroups = new ArrayList<>(); + SecurityGroup securityGroup = new SecurityGroup(); + securityGroup.setSecurityGroupName("test-SecurityGroupName"); + securityGroups.add(securityGroup); + response.setSecurityGroups(securityGroups); + return response; + } else if (argument instanceof DescribeInstancesRequest) { + DescribeInstancesResponse response = new DescribeInstancesResponse(); + List instances = new ArrayList<>(); + Instance instance = new Instance(); + instance.setInstanceName("test-InstanceName"); + instances.add(instance); + response.setInstances(instances); + return response; + } + return null; + } + } +} diff --git a/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/provider/agent/AliCloudImageCachingAgentTest.java b/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/provider/agent/AliCloudImageCachingAgentTest.java new file mode 100644 index 00000000000..c00d2d3ac1f --- /dev/null +++ b/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/provider/agent/AliCloudImageCachingAgentTest.java @@ -0,0 +1,78 @@ +/* + * Copyright 2022 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.clouddriver.alicloud.provider.agent; + +import static com.netflix.spinnaker.clouddriver.core.provider.agent.Namespace.IMAGES; +import static com.netflix.spinnaker.clouddriver.core.provider.agent.Namespace.NAMED_IMAGES; +import static org.junit.Assert.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; + +import com.aliyuncs.ecs.model.v20140526.DescribeImagesResponse; +import com.aliyuncs.ecs.model.v20140526.DescribeImagesResponse.Image; +import com.aliyuncs.exceptions.ClientException; +import com.netflix.spinnaker.cats.agent.CacheResult; +import com.netflix.spinnaker.cats.cache.CacheData; +import com.netflix.spinnaker.clouddriver.alicloud.cache.Keys; +import java.util.ArrayList; +import java.util.List; +import org.junit.Before; +import org.junit.Test; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; + +public class AliCloudImageCachingAgentTest extends CommonCachingAgentTest { + + private final String IMAGENAME = "imageName"; + private final String IMAGEID = "imageId"; + + @Before + public void testBefore() throws ClientException { + when(client.getAcsResponse(any())).thenAnswer(new ImageAnswer()); + } + + @Test + public void testLoadData() { + AliCloudImageCachingAgent agent = + new AliCloudImageCachingAgent(account, REGION, objectMapper, client); + CacheResult result = agent.loadData(providerCache); + String key = Keys.getImageKey(IMAGEID, ACCOUNT, REGION); + String nameKey = Keys.getNamedImageKey(ACCOUNT, IMAGENAME); + + List images = (List) result.getCacheResults().get(IMAGES.ns); + List namedImages = (List) result.getCacheResults().get(NAMED_IMAGES.ns); + + assertEquals(1, images.size()); + assertEquals(key, images.get(0).getId()); + + assertEquals(1, namedImages.size()); + assertEquals(nameKey, namedImages.get(0).getId()); + } + + private class ImageAnswer implements Answer { + @Override + public DescribeImagesResponse answer(InvocationOnMock invocation) throws Throwable { + DescribeImagesResponse response = new DescribeImagesResponse(); + List images = new ArrayList<>(); + Image image = new Image(); + image.setImageName(IMAGENAME); + image.setImageId(IMAGEID); + images.add(image); + response.setImages(images); + return response; + } + } +} diff --git a/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/provider/agent/AliCloudInstanceTypeCachingAgentTest.java b/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/provider/agent/AliCloudInstanceTypeCachingAgentTest.java new file mode 100644 index 00000000000..6506998c6bd --- /dev/null +++ b/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/provider/agent/AliCloudInstanceTypeCachingAgentTest.java @@ -0,0 +1,70 @@ +/* + * Copyright 2022 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.clouddriver.alicloud.provider.agent; + +import static org.junit.Assert.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; + +import com.aliyuncs.ecs.model.v20140526.DescribeAvailableResourceResponse; +import com.aliyuncs.ecs.model.v20140526.DescribeAvailableResourceResponse.AvailableZone; +import com.aliyuncs.exceptions.ClientException; +import com.netflix.spinnaker.cats.agent.CacheResult; +import com.netflix.spinnaker.cats.cache.CacheData; +import com.netflix.spinnaker.clouddriver.alicloud.cache.Keys; +import java.util.ArrayList; +import java.util.List; +import org.junit.Before; +import org.junit.Test; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; + +public class AliCloudInstanceTypeCachingAgentTest extends CommonCachingAgentTest { + + private final String ZONEID = "zoneId"; + + @Before + public void testBefore() throws ClientException { + when(client.getAcsResponse(any())).thenAnswer(new ZonesAnswer()); + } + + @Test + public void testLoadData() { + AliCloudInstanceTypeCachingAgent agent = + new AliCloudInstanceTypeCachingAgent(account, REGION, objectMapper, client); + CacheResult result = agent.loadData(providerCache); + String key = Keys.getInstanceTypeKey(ACCOUNT, REGION, ZONEID); + + List instanceTypes = + (List) result.getCacheResults().get(Keys.Namespace.INSTANCE_TYPES.ns); + + assertEquals(1, instanceTypes.size()); + assertEquals(key, instanceTypes.get(0).getId()); + } + + private class ZonesAnswer implements Answer { + @Override + public DescribeAvailableResourceResponse answer(InvocationOnMock invocation) throws Throwable { + DescribeAvailableResourceResponse response = new DescribeAvailableResourceResponse(); + List zones = new ArrayList<>(); + AvailableZone zone = new AvailableZone(); + zone.setZoneId(ZONEID); + zones.add(zone); + response.setAvailableZones(zones); + return response; + } + } +} diff --git a/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/provider/agent/AliCloudKeyPairCachingAgentTest.java b/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/provider/agent/AliCloudKeyPairCachingAgentTest.java new file mode 100644 index 00000000000..11d922ffced --- /dev/null +++ b/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/provider/agent/AliCloudKeyPairCachingAgentTest.java @@ -0,0 +1,71 @@ +/* + * Copyright 2022 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.clouddriver.alicloud.provider.agent; + +import static org.junit.Assert.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; + +import com.aliyuncs.ecs.model.v20140526.DescribeKeyPairsResponse; +import com.aliyuncs.ecs.model.v20140526.DescribeKeyPairsResponse.KeyPair; +import com.aliyuncs.exceptions.ClientException; +import com.netflix.spinnaker.cats.agent.CacheResult; +import com.netflix.spinnaker.cats.cache.CacheData; +import com.netflix.spinnaker.clouddriver.alicloud.cache.Keys; +import java.util.ArrayList; +import java.util.List; +import org.junit.Before; +import org.junit.Test; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; + +public class AliCloudKeyPairCachingAgentTest extends CommonCachingAgentTest { + + private final String KEYPAIRNAME = "kpName"; + + @Before + public void testBefore() throws ClientException { + when(client.getAcsResponse(any())).thenAnswer(new KeyPairsAnswer()); + } + + @Test + public void testLoadData() { + AliCloudKeyPairCachingAgent agent = + new AliCloudKeyPairCachingAgent(account, REGION, objectMapper, client); + CacheResult result = agent.loadData(providerCache); + + String key = Keys.getKeyPairKey(KEYPAIRNAME, REGION, ACCOUNT); + + List KeyPairs = + (List) result.getCacheResults().get(Keys.Namespace.ALI_CLOUD_KEY_PAIRS.ns); + + assertEquals(1, KeyPairs.size()); + assertEquals(key, KeyPairs.get(0).getId()); + } + + private class KeyPairsAnswer implements Answer { + @Override + public DescribeKeyPairsResponse answer(InvocationOnMock invocation) throws Throwable { + DescribeKeyPairsResponse response = new DescribeKeyPairsResponse(); + List keyPairs = new ArrayList<>(); + KeyPair keyPair = new KeyPair(); + keyPair.setKeyPairName(KEYPAIRNAME); + keyPairs.add(keyPair); + response.setKeyPairs(keyPairs); + return response; + } + } +} diff --git a/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/provider/agent/AliCloudLoadBalancerCachingAgentTest.java b/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/provider/agent/AliCloudLoadBalancerCachingAgentTest.java index 0c236e4c8dc..a78ce249206 100644 --- a/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/provider/agent/AliCloudLoadBalancerCachingAgentTest.java +++ b/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/provider/agent/AliCloudLoadBalancerCachingAgentTest.java @@ -5,7 +5,7 @@ * 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 + * 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, diff --git a/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/provider/agent/AliCloudSecurityGroupCachingAgentTest.java b/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/provider/agent/AliCloudSecurityGroupCachingAgentTest.java index ad3c0bed1d6..4eab15930da 100644 --- a/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/provider/agent/AliCloudSecurityGroupCachingAgentTest.java +++ b/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/provider/agent/AliCloudSecurityGroupCachingAgentTest.java @@ -5,7 +5,7 @@ * 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 + * 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, diff --git a/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/provider/agent/AliCloudSubnetCachingAgentTest.java b/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/provider/agent/AliCloudSubnetCachingAgentTest.java new file mode 100644 index 00000000000..673dd0b91b1 --- /dev/null +++ b/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/provider/agent/AliCloudSubnetCachingAgentTest.java @@ -0,0 +1,85 @@ +/* + * Copyright 2022 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.clouddriver.alicloud.provider.agent; + +import static com.netflix.spinnaker.clouddriver.alicloud.cache.Keys.Namespace.SUBNETS; +import static org.junit.Assert.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; + +import com.aliyuncs.exceptions.ClientException; +import com.aliyuncs.vpc.model.v20160428.DescribeVSwitchesResponse; +import com.aliyuncs.vpc.model.v20160428.DescribeVSwitchesResponse.VSwitch; +import com.aliyuncs.vpc.model.v20160428.DescribeVpcsResponse; +import com.aliyuncs.vpc.model.v20160428.DescribeVpcsResponse.Vpc; +import com.netflix.spinnaker.cats.agent.CacheResult; +import com.netflix.spinnaker.cats.cache.CacheData; +import com.netflix.spinnaker.clouddriver.alicloud.cache.Keys; +import java.util.ArrayList; +import java.util.List; +import org.junit.Before; +import org.junit.Test; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; + +public class AliCloudSubnetCachingAgentTest extends CommonCachingAgentTest { + + private final String VSWITCHID = "1234567890"; + + @Before + public void testBefore() throws ClientException { + when(client.getAcsResponse(any())) + .thenAnswer(new CustomAnswer()) + .thenAnswer(new DescribeVpcsAnswer()); + } + + @Test + public void testLoadData() { + AliCloudSubnetCachingAgent agent = + new AliCloudSubnetCachingAgent(account, REGION, objectMapper, client); + CacheResult result = agent.loadData(providerCache); + String key = Keys.getSubnetKey(VSWITCHID, REGION, ACCOUNT); + List subnets = (List) result.getCacheResults().get(SUBNETS.ns); + assertEquals(1, subnets.size()); + assertEquals(key, subnets.get(0).getId()); + } + + private class CustomAnswer implements Answer { + @Override + public DescribeVSwitchesResponse answer(InvocationOnMock invocation) throws Throwable { + DescribeVSwitchesResponse describeVSwitchesResponse = new DescribeVSwitchesResponse(); + List vSwitches = new ArrayList<>(); + VSwitch vSwitch = new VSwitch(); + vSwitch.setVSwitchId(VSWITCHID); + vSwitches.add(vSwitch); + describeVSwitchesResponse.setVSwitches(vSwitches); + return describeVSwitchesResponse; + } + } + + private class DescribeVpcsAnswer implements Answer { + @Override + public DescribeVpcsResponse answer(InvocationOnMock invocation) throws Throwable { + DescribeVpcsResponse response = new DescribeVpcsResponse(); + List vpcs = new ArrayList<>(); + Vpc vpc = new Vpc(); + vpc.setVpcName("test-VpcName"); + vpcs.add(vpc); + response.setVpcs(vpcs); + return response; + } + } +} diff --git a/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/provider/agent/CommonCachingAgentTest.java b/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/provider/agent/CommonCachingAgentTest.java index be551c8f7eb..d769d32fe04 100644 --- a/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/provider/agent/CommonCachingAgentTest.java +++ b/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/provider/agent/CommonCachingAgentTest.java @@ -5,7 +5,7 @@ * 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 + * 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, @@ -21,13 +21,11 @@ import com.aliyuncs.IAcsClient; import com.fasterxml.jackson.databind.ObjectMapper; import com.netflix.spinnaker.cats.provider.ProviderCache; +import com.netflix.spinnaker.clouddriver.alicloud.BaseTest; import com.netflix.spinnaker.clouddriver.alicloud.security.AliCloudCredentials; import spock.lang.Subject; -public class CommonCachingAgentTest { - - static final String ACCOUNT = "test-account"; - static final String REGION = "cn-test"; +public class CommonCachingAgentTest extends BaseTest { @Subject ObjectMapper objectMapper = new ObjectMapper(); diff --git a/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/provider/view/AliCloudClusterProviderTest.java b/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/provider/view/AliCloudClusterProviderTest.java new file mode 100644 index 00000000000..dc1e52e34da --- /dev/null +++ b/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/provider/view/AliCloudClusterProviderTest.java @@ -0,0 +1,170 @@ +/* + * Copyright 2022 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.clouddriver.alicloud.provider.view; + +import static com.netflix.spinnaker.clouddriver.core.provider.agent.Namespace.APPLICATIONS; +import static com.netflix.spinnaker.clouddriver.core.provider.agent.Namespace.CLUSTERS; +import static com.netflix.spinnaker.clouddriver.core.provider.agent.Namespace.SERVER_GROUPS; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.mockito.ArgumentMatchers.anyCollection; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.when; + +import com.google.common.collect.Lists; +import com.netflix.spinnaker.cats.cache.CacheData; +import com.netflix.spinnaker.cats.cache.DefaultCacheData; +import com.netflix.spinnaker.clouddriver.alicloud.AliCloudProvider; +import com.netflix.spinnaker.clouddriver.alicloud.model.AliCloudCluster; +import com.netflix.spinnaker.clouddriver.alicloud.model.AliCloudServerGroup; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; +import org.jetbrains.annotations.NotNull; +import org.junit.Before; +import org.junit.Test; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; + +public class AliCloudClusterProviderTest extends CommonProvider { + + @Before + public void testBefore() {} + + @Test + public void testGetClusterDetails() { + when(cacheView.get(eq(APPLICATIONS.ns), anyString())).thenAnswer(new ApplicationAnswer()); + when(cacheView.getAll(eq(CLUSTERS.ns), anyCollection())).thenAnswer(new ClustersAnswer()); + when(cacheView.getAll(eq(SERVER_GROUPS.ns), anyCollection())) + .thenAnswer(new ServerGroupsAnswer()); + + AliCloudClusterProvider provider = + new AliCloudClusterProvider(objectMapper, cacheView, new AliCloudProvider()); + Map> clusterDetails = + provider.getClusterDetails("test-application"); + assertEquals(1, clusterDetails.get("test-application").size()); + } + + @Test + public void testGetCluster() { + when(cacheView.get(eq(CLUSTERS.ns), anyString())).thenReturn(clusterCacheData()); + when(cacheView.getAll(eq(SERVER_GROUPS.ns), anyCollection())) + .thenAnswer(new ServerGroupsAnswer()); + + AliCloudClusterProvider provider = + new AliCloudClusterProvider(objectMapper, cacheView, new AliCloudProvider()); + AliCloudCluster cluster = + provider.getCluster("test-application", ACCOUNT, "test-cluster", true); + assertNotNull(cluster); + } + + @Test + public void testGetServerGroup() { + when(cacheView.get(eq(SERVER_GROUPS.ns), anyString())).thenReturn(serverGroupCacheData()); + + AliCloudClusterProvider provider = + new AliCloudClusterProvider(objectMapper, cacheView, new AliCloudProvider()); + AliCloudServerGroup serverGroup = + provider.getServerGroup(ACCOUNT, REGION, "test-cluster", true); + assertNotNull(serverGroup); + } + + private class ServerGroupsAnswer implements Answer> { + @Override + public Collection answer(InvocationOnMock invocation) throws Throwable { + CacheData cacheData = serverGroupCacheData(); + return Lists.newArrayList(cacheData); + } + } + + @NotNull + private CacheData serverGroupCacheData() { + Map attributes = new HashMap<>(); + attributes.put("account", ACCOUNT); + attributes.put("name", "test-serverGroupName"); + attributes.put("region", REGION); + + Map scalingGroup = new HashMap<>(); + scalingGroup.put("lifecycleState", "Active"); + scalingGroup.put("maxSize", 10); + scalingGroup.put("minSize", 1); + scalingGroup.put("creationTime", "2022-04-01T16:20Z"); + attributes.put("scalingGroup", scalingGroup); + + List instances = new ArrayList<>(); + Map instance = new HashMap<>(); + instance.put("instanceId", "test-instanceId"); + instance.put("healthStatus", "Healthy"); + + instances.add(instance); + attributes.put("instances", instances); + + Map scalingConfiguration = new HashMap<>(); + scalingConfiguration.put("imageId", "test-imageId"); + attributes.put("scalingConfiguration", scalingConfiguration); + + CacheData cacheData = + new DefaultCacheData( + "alicloud:serverGroups:test-serverGroup:test-account:cn-hangzhou:test-serverGroup", + attributes, + null); + return cacheData; + } + + private class ClustersAnswer implements Answer> { + @Override + public Collection answer(InvocationOnMock invocation) throws Throwable { + CacheData cacheData = clusterCacheData(); + return Lists.newArrayList(cacheData); + } + } + + @NotNull + private CacheData clusterCacheData() { + Map attributes = new HashMap<>(); + attributes.put("application", "test-application"); + + Map> relationships = new HashMap<>(); + List serverGroupList = new ArrayList<>(); + serverGroupList.add( + "alicloud:serverGroups:test-serverGroup:test-account:cn-hangzhou:test-serverGroup"); + relationships.put(SERVER_GROUPS.ns, serverGroupList); + + CacheData cacheData = + new DefaultCacheData( + "alicloud:clusters:test-cluster:test-account:test-cluster", attributes, relationships); + return cacheData; + } + + private class ApplicationAnswer implements Answer { + @Override + public CacheData answer(InvocationOnMock invocation) throws Throwable { + Map attributes = new HashMap<>(); + attributes.put("name", "test-application"); + Map> relationships = new HashMap<>(); + List clusterList = new ArrayList<>(); + clusterList.add("alicloud:clusters:test-cluster:test-account:test-cluster"); + relationships.put(CLUSTERS.ns, clusterList); + CacheData cacheData = + new DefaultCacheData("alicloud:applications:test-application", attributes, relationships); + return cacheData; + } + } +} diff --git a/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/provider/view/AliCloudInstanceTypeProviderTest.java b/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/provider/view/AliCloudInstanceTypeProviderTest.java new file mode 100644 index 00000000000..c644a7528c1 --- /dev/null +++ b/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/provider/view/AliCloudInstanceTypeProviderTest.java @@ -0,0 +1,80 @@ +/* + * Copyright 2022 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.clouddriver.alicloud.provider.view; + +import static org.junit.Assert.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.when; + +import com.netflix.spinnaker.cats.cache.CacheData; +import com.netflix.spinnaker.cats.cache.DefaultCacheData; +import com.netflix.spinnaker.clouddriver.alicloud.provider.view.AliCloudInstanceTypeProvider.Zone; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; +import org.junit.Before; +import org.junit.Test; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; + +public class AliCloudInstanceTypeProviderTest extends CommonProvider { + + @Before + public void testBefore() { + when(cacheView.filterIdentifiers(anyString(), anyString())).thenAnswer(new FilterAnswer()); + when(cacheView.getAll(anyString(), any(), any())).thenAnswer(new CacheDataAnswer()); + } + + @Test + public void testGetAll() { + AliCloudInstanceTypeProvider provider = + new AliCloudInstanceTypeProvider(objectMapper, cacheView); + Set all = provider.getAll(); + assertEquals(1, all.size()); + } + + private class FilterAnswer implements Answer> { + @Override + public List answer(InvocationOnMock invocation) throws Throwable { + List list = new ArrayList<>(); + list.add("alicloud:instanceTypes:test-account:cn-hangzhou:cn-hangzhou-h"); + return list; + } + } + + private class CacheDataAnswer implements Answer> { + @Override + public List answer(InvocationOnMock invocation) throws Throwable { + List cacheDatas = new ArrayList<>(); + Map attributes = new HashMap<>(); + attributes.put("account", ACCOUNT); + attributes.put("regionId", REGION); + attributes.put("zoneId", "cn-hangzhou-f"); + attributes.put("provider", "alicloud"); + List availableInstanceTypes = new ArrayList<>(); + availableInstanceTypes.add("xxxxxx"); + attributes.put("availableInstanceTypes", availableInstanceTypes); + CacheData cacheData1 = + new DefaultCacheData( + "alicloud:instanceTypes:test-account:cn-hangzhou:cn-hangzhou-h", attributes, null); + cacheDatas.add(cacheData1); + return cacheDatas; + } + } +} diff --git a/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/provider/view/AliCloudKeyPairProviderTest.java b/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/provider/view/AliCloudKeyPairProviderTest.java new file mode 100644 index 00000000000..d08f0be9891 --- /dev/null +++ b/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/provider/view/AliCloudKeyPairProviderTest.java @@ -0,0 +1,76 @@ +/* + * Copyright 2022 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.clouddriver.alicloud.provider.view; + +import static org.junit.Assert.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.when; + +import com.netflix.spinnaker.cats.cache.CacheData; +import com.netflix.spinnaker.cats.cache.DefaultCacheData; +import com.netflix.spinnaker.clouddriver.alicloud.model.AliCloudKeyPair; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; +import org.junit.Before; +import org.junit.Test; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; + +public class AliCloudKeyPairProviderTest extends CommonProvider { + + @Before + public void testBefore() { + when(cacheView.filterIdentifiers(anyString(), anyString())).thenAnswer(new FilterAnswer()); + when(cacheView.getAll(anyString(), any(), any())).thenAnswer(new CacheDataAnswer()); + } + + @Test + public void testGetAll() { + AliCloudKeyPairProvider provider = new AliCloudKeyPairProvider(objectMapper, cacheView); + Set all = provider.getAll(); + assertEquals(1, all.size()); + } + + private class FilterAnswer implements Answer> { + @Override + public List answer(InvocationOnMock invocation) throws Throwable { + List list = new ArrayList<>(); + list.add("alicloud:keyPairs:test-keyPair:test-account:cn-hangzhou"); + return list; + } + } + + private class CacheDataAnswer implements Answer> { + @Override + public List answer(InvocationOnMock invocation) throws Throwable { + List cacheDatas = new ArrayList<>(); + Map attributes = new HashMap<>(); + attributes.put("account", ACCOUNT); + attributes.put("regionId", REGION); + attributes.put("keyPairName", "kpName"); + attributes.put("keyPairFingerPrint", "kpFingerPrint"); + CacheData cacheData1 = + new DefaultCacheData( + "alicloud:keyPairs:test-keyPair:test-account:cn-hangzhou", attributes, null); + cacheDatas.add(cacheData1); + return cacheDatas; + } + } +} diff --git a/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/provider/view/AliCloudLoadBalancerProviderTest.java b/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/provider/view/AliCloudLoadBalancerProviderTest.java index 2a9aa5495e3..fc3dc97f981 100644 --- a/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/provider/view/AliCloudLoadBalancerProviderTest.java +++ b/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/provider/view/AliCloudLoadBalancerProviderTest.java @@ -15,71 +15,121 @@ */ package com.netflix.spinnaker.clouddriver.alicloud.provider.view; -import static org.junit.Assert.assertTrue; -import static org.mockito.ArgumentMatchers.any; +import static com.netflix.spinnaker.clouddriver.core.provider.agent.Namespace.APPLICATIONS; +import static com.netflix.spinnaker.clouddriver.core.provider.agent.Namespace.CLUSTERS; +import static com.netflix.spinnaker.clouddriver.core.provider.agent.Namespace.LOAD_BALANCERS; +import static com.netflix.spinnaker.clouddriver.core.provider.agent.Namespace.SERVER_GROUPS; +import static org.junit.Assert.assertEquals; +import static org.mockito.ArgumentMatchers.anyCollection; import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.when; +import com.aliyuncs.slb.model.v20140515.DescribeLoadBalancerAttributeResponse; +import com.aliyuncs.slb.model.v20140515.DescribeLoadBalancersResponse.LoadBalancer; +import com.aliyuncs.slb.model.v20140515.DescribeVServerGroupsResponse.VServerGroup; +import com.google.common.collect.Lists; +import com.netflix.frigga.Names; import com.netflix.spinnaker.cats.cache.CacheData; import com.netflix.spinnaker.cats.cache.DefaultCacheData; +import com.netflix.spinnaker.clouddriver.alicloud.cache.Keys; +import com.netflix.spinnaker.clouddriver.alicloud.common.Sets; import com.netflix.spinnaker.clouddriver.alicloud.model.AliCloudLoadBalancer; +import com.netflix.spinnaker.clouddriver.alicloud.provider.agent.AliCloudLoadBalancerCachingAgent.LoadBalancerCacheBuilder; import com.netflix.spinnaker.clouddriver.alicloud.provider.view.AliCloudLoadBalancerProvider.ResultDetails; -import java.util.*; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; import org.junit.Before; import org.junit.Test; -import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; public class AliCloudLoadBalancerProviderTest extends CommonProvider { + private String applicationName = "test-application"; + private String clusterKey; + private String serverGroupKey; + LoadBalancer loadBalancer = fromMockData("mock/loadbalancer.json", LoadBalancer.class); @Before public void testBefore() { - when(cacheView.getIdentifiers(anyString())).thenAnswer(new FilterAnswer()); - when(cacheView.getAll(anyString(), any(), any())).thenAnswer(new CacheDataAnswer()); + String scalingGroupName = "test-application-fromspinnaker-test-v003"; + Names name = Names.parseName(scalingGroupName); + applicationName = name.getApp(); + clusterKey = Keys.getClusterKey(name.getCluster(), name.getApp(), ACCOUNT); + serverGroupKey = Keys.getServerGroupKey(scalingGroupName, ACCOUNT, REGION); + + when(cacheView.get(eq(APPLICATIONS.ns), eq(Keys.getApplicationKey(applicationName)))) + .thenAnswer( + (Answer) + invocation -> { + Map attributes = new HashMap<>(); + attributes.put("name", applicationName); + + Map> relationships = new HashMap<>(16); + relationships.put(CLUSTERS.ns, Sets.ofModifiable(clusterKey)); + relationships.put(SERVER_GROUPS.ns, Sets.ofModifiable(serverGroupKey)); + relationships.put(LOAD_BALANCERS.ns, Sets.ofModifiable(serverGroupKey)); + return new DefaultCacheData( + Keys.getApplicationKey(applicationName), attributes, relationships); + }); + + when(cacheView.getAll(eq(LOAD_BALANCERS.ns), anyCollection())) + .thenAnswer( + (Answer>) + invocation -> { + DescribeLoadBalancerAttributeResponse loadBalancerAttribute = + fromMockData( + "mock/describeLoadBalancerAttributeResponse.json", + DescribeLoadBalancerAttributeResponse.class); + loadBalancerAttribute.setLoadBalancerName(loadBalancer.getLoadBalancerName()); + Map listenerAttribute = fromMockData("mock/listenerAttributes.json", Map.class); + VServerGroup vServerGroup = + fromMockData("mock/vserverGroup.json", VServerGroup.class); + return Lists.newArrayList( + new LoadBalancerCacheBuilder( + loadBalancer, + loadBalancerAttribute, + Lists.newArrayList(listenerAttribute), + Lists.newArrayList(vServerGroup), + ACCOUNT, + REGION) + .buildLoadBalancerCache(objectMapper)); + }); + + when(cacheView.getAll(eq(SERVER_GROUPS.ns), anyCollection())) + .thenAnswer((Answer>) invocation -> Collections.EMPTY_LIST); } @Test public void testGetApplicationLoadBalancers() { + AliCloudLoadBalancerProvider provider = new AliCloudLoadBalancerProvider(objectMapper, cacheView, oldProvider); + Set applicationLoadBalancers = - provider.getApplicationLoadBalancers("test-application"); - assertTrue(applicationLoadBalancers.size() == 1); + provider.getApplicationLoadBalancers(applicationName); + assertEquals(1, applicationLoadBalancers.size()); + AliCloudLoadBalancer aliCloudLoadBalancer = applicationLoadBalancers.iterator().next(); + assertEquals(aliCloudLoadBalancer.getLoadBalancerId(), loadBalancer.getLoadBalancerId()); + assertEquals(aliCloudLoadBalancer.getAccount(), ACCOUNT); + assertEquals(aliCloudLoadBalancer.getRegion(), loadBalancer.getRegionIdAlias()); + assertEquals(aliCloudLoadBalancer.getVpcId(), loadBalancer.getVpcId()); } @Test public void testByAccountAndRegionAndName() { + String loadBalancer = "lbName"; AliCloudLoadBalancerProvider provider = new AliCloudLoadBalancerProvider(objectMapper, cacheView, oldProvider); - List lbName = provider.byAccountAndRegionAndName(ACCOUNT, REGION, "lbName"); - assertTrue(lbName.size() == 1); - } - private class FilterAnswer implements Answer> { - @Override - public List answer(InvocationOnMock invocation) throws Throwable { - List list = new ArrayList<>(); - list.add("alicloud:loadBalancers:test-account:cn-hangzhou:test-application"); - return list; - } - } + when(cacheView.filterIdentifiers(eq(LOAD_BALANCERS.ns), anyString())) + .thenReturn(Lists.newArrayList("test-key")); + + List lbName = provider.byAccountAndRegionAndName(ACCOUNT, REGION, loadBalancer); - private class CacheDataAnswer implements Answer> { - @Override - public List answer(InvocationOnMock invocation) throws Throwable { - List cacheDatas = new ArrayList<>(); - Map attributes = new HashMap<>(); - attributes.put("account", ACCOUNT); - attributes.put("regionId", REGION); - attributes.put("regionIdAlias", "test-alias"); - attributes.put("loadBalancerName", "lbName"); - attributes.put("vpcId", "test-vpcId"); - attributes.put("loadBalancerId", "lbId"); - CacheData cacheData1 = - new DefaultCacheData( - "alicloud:loadBalancers:test-account:cn-hangzhou:test-application", attributes, null); - cacheDatas.add(cacheData1); - return cacheDatas; - } + assertEquals(1, lbName.size()); } } diff --git a/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/provider/view/AliCloudSecurityGroupProviderTest.java b/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/provider/view/AliCloudSecurityGroupProviderTest.java index b6a9e4fe2f0..8635aa55163 100644 --- a/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/provider/view/AliCloudSecurityGroupProviderTest.java +++ b/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/provider/view/AliCloudSecurityGroupProviderTest.java @@ -5,7 +5,7 @@ * 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 + * 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, @@ -15,7 +15,7 @@ */ package com.netflix.spinnaker.clouddriver.alicloud.provider.view; -import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertEquals; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.when; @@ -23,7 +23,11 @@ import com.netflix.spinnaker.cats.cache.CacheData; import com.netflix.spinnaker.cats.cache.DefaultCacheData; import com.netflix.spinnaker.clouddriver.alicloud.model.AliCloudSecurityGroup; -import java.util.*; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.List; +import java.util.Map; import org.junit.Before; import org.junit.Test; import org.mockito.invocation.InvocationOnMock; @@ -42,7 +46,7 @@ public void testGetAllByAccount() { AliCloudSecurityGroupProvider provider = new AliCloudSecurityGroupProvider(objectMapper, cacheView); Collection allByAccounts = provider.getAllByAccount(true, ACCOUNT); - assertTrue(allByAccounts.size() == 1); + assertEquals(1, allByAccounts.size()); } private class FilterAnswer implements Answer> { diff --git a/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/provider/view/AliCloudSubnetProviderTest.java b/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/provider/view/AliCloudSubnetProviderTest.java new file mode 100644 index 00000000000..109c8298394 --- /dev/null +++ b/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/provider/view/AliCloudSubnetProviderTest.java @@ -0,0 +1,80 @@ +/* + * Copyright 2022 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.clouddriver.alicloud.provider.view; + +import static org.junit.Assert.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.when; + +import com.netflix.spinnaker.cats.cache.CacheData; +import com.netflix.spinnaker.cats.cache.DefaultCacheData; +import com.netflix.spinnaker.clouddriver.alicloud.model.AliCloudSubnet; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; +import org.junit.Before; +import org.junit.Test; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; + +public class AliCloudSubnetProviderTest extends CommonProvider { + + @Before + public void testBefore() { + when(cacheView.filterIdentifiers(anyString(), anyString())).thenAnswer(new FilterAnswer()); + when(cacheView.getAll(anyString(), any(), any())).thenAnswer(new CacheDataAnswer()); + } + + @Test + public void testGetAll() { + AliCloudSubnetProvider provider = new AliCloudSubnetProvider(objectMapper, cacheView); + Set all = provider.getAll(); + assertEquals(1, all.size()); + } + + private class FilterAnswer implements Answer> { + @Override + public List answer(InvocationOnMock invocation) throws Throwable { + List list = new ArrayList<>(); + list.add("alicloud:subnets:vsw-test:test-account:cn-hangzhou"); + return list; + } + } + + private class CacheDataAnswer implements Answer> { + @Override + public List answer(InvocationOnMock invocation) throws Throwable { + List cacheDatas = new ArrayList<>(); + Map attributes = new HashMap<>(); + attributes.put("account", ACCOUNT); + attributes.put("region", REGION); + attributes.put("status", "Available"); + attributes.put("vpcId", "vpc-test"); + attributes.put("zoneId", "cn-hangzhou-f"); + attributes.put("type", "alicloud"); + attributes.put("vswitchId", "vId"); + attributes.put("vswitchName", "vName"); + CacheData cacheData1 = + new DefaultCacheData( + "alicloud:subnets:vsw-test:test-account:cn-hangzhou", attributes, null); + cacheDatas.add(cacheData1); + return cacheDatas; + } + } +} diff --git a/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/provider/view/CommonProvider.java b/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/provider/view/CommonProvider.java index 257c5b1f05c..1ccb517e2db 100644 --- a/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/provider/view/CommonProvider.java +++ b/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/provider/view/CommonProvider.java @@ -5,7 +5,7 @@ * 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 + * 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, @@ -20,12 +20,10 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.netflix.spinnaker.cats.cache.Cache; import com.netflix.spinnaker.clouddriver.alicloud.AliCloudProvider; +import com.netflix.spinnaker.clouddriver.alicloud.BaseTest; import spock.lang.Subject; -public class CommonProvider { - - static final String ACCOUNT = "test-account"; - static final String REGION = "cn-test"; +public class CommonProvider extends BaseTest { @Subject ObjectMapper objectMapper = new ObjectMapper(); diff --git a/clouddriver-alicloud/src/test/resources/mock/describeLoadBalancerAttributeResponse.json b/clouddriver-alicloud/src/test/resources/mock/describeLoadBalancerAttributeResponse.json new file mode 100644 index 00000000000..5d7b558ebf4 --- /dev/null +++ b/clouddriver-alicloud/src/test/resources/mock/describeLoadBalancerAttributeResponse.json @@ -0,0 +1,56 @@ +{ + "requestId": "D961E7D1-E1C2-5B8A-813C-996CCA04BCAC", + "loadBalancerId": "lb-bp14zp0z9a14keun50qed", + "resourceGroupId": "rg-acfmwvvtg5owavy", + "loadBalancerName": "ManagedK8SSlbIntranet-ca6a8ff0dd3c4471fb22b0b308afdbae7", + "loadBalancerStatus": "active", + "regionId": "cn-hangzhou", + "regionIdAlias": "cn-hangzhou", + "address": "172.16.6.153", + "addressType": "intranet", + "vpcId": "vpc-bp1v8dqu75hsxns2vh4l0", + "vSwitchId": "vsw-bp1ov7as4qoh1b7qrhgpq", + "networkType": "vpc", + "internetChargeType": "paybytraffic", + "bandwidth": 5120, + "loadBalancerSpec": "slb.s2.small", + "createTime": "2022-04-07T09:39:56Z", + "createTimeStamp": 1649324396000, + "endTime": "2999-09-08T16:00:00Z", + "endTimeStamp": 32493801600000, + "payType": "PayOnDemand", + "masterZoneId": "cn-hangzhou-g", + "slaveZoneId": "cn-hangzhou-b", + "addressIPVersion": "ipv4", + "hasReservedInfo": "false", + "deleteProtection": "on", + "listenerPortsAndProtocal": [ + { + "listenerPort": 6443, + "listenerProtocal": "tcp" + } + ], + "listenerPortsAndProtocol": [ + { + "listenerPort": 6443, + "listenerProtocol": "tcp" + } + ], + "backendServers": [ + { + "serverId": "eni-bp1gagggeuuk902bmzjq", + "weight": 100, + "type": "eni", + "serverIp": "172.16.6.155" + }, + { + "serverId": "eni-bp177qrxy534lgvcgq2r", + "weight": 100, + "type": "eni", + "serverIp": "172.16.6.154" + } + ], + "listenerPorts": [ + "6443" + ] +} \ No newline at end of file diff --git a/clouddriver-alicloud/src/test/resources/mock/describeScalingConfigurationsResponse.json b/clouddriver-alicloud/src/test/resources/mock/describeScalingConfigurationsResponse.json new file mode 100644 index 00000000000..e69de29bb2d diff --git a/clouddriver-alicloud/src/test/resources/mock/listenerAttributes.json b/clouddriver-alicloud/src/test/resources/mock/listenerAttributes.json new file mode 100644 index 00000000000..f6a323d1094 --- /dev/null +++ b/clouddriver-alicloud/src/test/resources/mock/listenerAttributes.json @@ -0,0 +1,20 @@ +{ + "aclStatus": "off", + "backendServerPort": 6443, + "bandwidth": -1, + "establishedTimeout": 900, + "healthCheck": "on", + "healthCheckConnectTimeout": 5, + "healthCheckDomain": "", + "healthCheckHttpCode": "", + "healthCheckInterval": 2, + "healthCheckType": "tcp", + "healthCheckURI": "", + "healthyThreshold": 3, + "listenerPort": 6443, + "persistenceTimeout": 0, + "requestId": "392A1C98-980B-5F4A-96AA-F03CCFAFA25A", + "scheduler": "wrr", + "status": "running", + "unhealthyThreshold": 3 +} \ No newline at end of file diff --git a/clouddriver-alicloud/src/test/resources/mock/loadbalancer.json b/clouddriver-alicloud/src/test/resources/mock/loadbalancer.json new file mode 100644 index 00000000000..ef628afd940 --- /dev/null +++ b/clouddriver-alicloud/src/test/resources/mock/loadbalancer.json @@ -0,0 +1,32 @@ +{ + "loadBalancerId": "lb-bp14zp0z9a14keun50qed", + "loadBalancerName": "test-loadBalancerName", + "loadBalancerStatus": "active", + "address": "172.16.6.153", + "addressType": "intranet", + "regionId": "cn-hangzhou", + "regionIdAlias": "cn-hangzhou", + "vSwitchId": "vsw-bp1ov7as4qoh1b7qrhgpq", + "vpcId": "vpc-bp1v8dqu75hsxns2vh4l0", + "networkType": "vpc", + "masterZoneId": "cn-hangzhou-g", + "slaveZoneId": "cn-hangzhou-b", + "internetChargeType": "4", + "createTime": "2022-04-07T17:39Z", + "createTimeStamp": 1649324396000, + "payType": "PayOnDemand", + "resourceGroupId": "rg-acfmwvvtg5owavy", + "addressIPVersion": "ipv4", + "tags": [ + { + "tagKey": "ack.aliyun.com", + "tagValue": "ca6a8ff0dd3c4471fb22b0b308afdbae7" + } + ] +} + + + + + + diff --git a/clouddriver-alicloud/src/test/resources/mock/scalingGroup.json b/clouddriver-alicloud/src/test/resources/mock/scalingGroup.json new file mode 100644 index 00000000000..e69de29bb2d diff --git a/clouddriver-alicloud/src/test/resources/mock/scalingInstance.json b/clouddriver-alicloud/src/test/resources/mock/scalingInstance.json new file mode 100644 index 00000000000..e69de29bb2d diff --git a/clouddriver-alicloud/src/test/resources/mock/vserverGroup.json b/clouddriver-alicloud/src/test/resources/mock/vserverGroup.json new file mode 100644 index 00000000000..0a6d8cd6777 --- /dev/null +++ b/clouddriver-alicloud/src/test/resources/mock/vserverGroup.json @@ -0,0 +1,8 @@ +{ + "vServerGroupId": "rsp-bp1p4kznmr6y9", + "vServerGroupName": "tf-server-group", + "associatedObjects": { + "listeners": [], + "rules": [] + } +} \ No newline at end of file From 9f134206280a85104692bb288cd42326fc525ee8 Mon Sep 17 00:00:00 2001 From: "zigang.wang" Date: Wed, 15 Jun 2022 10:59:57 +0800 Subject: [PATCH 2/2] feat(alicloud): cluster ops MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Supports sts - Supports servergroup、loadbalancer、securityGroup ops --- clouddriver-alicloud/README.md | 22 + .../clouddriver-alicloud.gradle | 6 +- .../clouddriver/alicloud/cache/Keys.java | 15 +- .../AliCloudServerGroupNameResolver.java | 94 ++++ ...udServerGroupAtomicOperationConverter.java | 61 +++ ...udServerGroupAtomicOperationConverter.java | 102 ++++ ...SecurityGroupAtomicOperationConverter.java | 2 +- ...udServerGroupAtomicOperationConverter.java | 54 ++ ...udServerGroupAtomicOperationConverter.java | 54 ++ ...udServerGroupAtomicOperationConverter.java | 54 ++ ...yScalingGroupAtomicOperationConverter.java | 52 ++ ...udServerGroupAtomicOperationConverter.java | 54 ++ ...dLaunchConfigAtomicOperationConverter.java | 55 ++ ...loudImageTagsAtomicOperationConverter.java | 55 ++ ...SecurityGroupAtomicOperationConverter.java | 2 +- .../description/BaseAliCloudDescription.java | 3 +- .../BasicAliCloudDeployDescription.java | 85 ++++ ...eleteAliCloudSecurityGroupDescription.java | 2 + ...DestroyAliCloudServerGroupDescription.java | 29 ++ ...DisableAliCloudServerGroupDescription.java | 29 ++ ...EnableAliCloudServerGroupDescription.java} | 18 +- .../ModifyScalingGroupDescription.java | 72 +++ .../ResizeAliCloudServerGroupDescription.java | 38 ++ ...UpdateAliCloudLaunchConfigDescription.java | 126 +++++ .../UpsertAliCloudImageTagsDescription.java | 29 ++ ...UpsertAliCloudLoadBalancerDescription.java | 10 +- ...psertAliCloudSecurityGroupDescription.java | 2 + ...opyAliCloudServerGroupAtomicOperation.java | 470 ++++++++++++++++++ ...ateAliCloudServerGroupAtomicOperation.java | 426 ++++++++++++++++ ...oudLoadBalancerClassicAtomicOperation.java | 11 +- ...eAliCloudSecurityGroupAtomicOperation.java | 19 +- ...royAliCloudServerGroupAtomicOperation.java | 83 ++++ ...bleAliCloudServerGroupAtomicOperation.java | 109 ++++ ...bleAliCloudServerGroupAtomicOperation.java | 90 ++++ .../ModifyScalingGroupAtomicOperation.java | 87 ++++ ...izeAliCloudServerGroupAtomicOperation.java | 77 +++ ...teAliCloudLaunchConfigAtomicOperation.java | 85 ++++ ...psertAliCloudImageTagsAtomicOperation.java | 91 ++++ ...rtAliCloudLoadBalancerAtomicOperation.java | 33 +- ...tAliCloudSecurityGroupAtomicOperation.java | 26 +- .../alicloud/model/AliCloudLoadBalancer.java | 1 + .../model/AliCloudLoadBalancerType.java | 13 +- .../agent/AliCloudClusterCachingAgent.java | 35 +- .../AliCloudLoadBalancerCachingAgent.java | 11 +- .../view/AliCloudLoadBalancerProvider.java | 19 +- .../security/AliCloudCredentials.java | 66 +-- .../AliCloudCredentialsInitializer.java | 65 ++- .../config/AliCloudAccountConfig.java | 23 +- .../clouddriver/alicloud/cache/KeysTest.java | 31 +- .../AliCloudImageControllerTest.java | 13 +- ...iCloudScalingActivitiesControllerTest.java | 2 +- .../deploy/converters/CommonConverter.java | 21 +- ...rverGroupAtomicOperationConverterTest.java | 59 +++ ...dBalancerAtomicOperationConverterTest.java | 2 +- ...rityGroupAtomicOperationConverterTest.java | 2 +- ...rverGroupAtomicOperationConverterTest.java | 60 +++ ...rverGroupAtomicOperationConverterTest.java | 60 +++ ...rverGroupAtomicOperationConverterTest.java | 60 +++ ...lingGroupAtomicOperationConverterTest.java | 59 +++ ...rverGroupAtomicOperationConverterTest.java | 60 +++ ...nchConfigAtomicOperationConverterTest.java | 60 +++ ...dBalancerAtomicOperationConverterTest.java | 2 +- ...rityGroupAtomicOperationConverterTest.java | 2 +- .../deploy/ops/CommonAtomicOperation.java | 21 +- ...liCloudServerGroupAtomicOperationTest.java | 94 ++++ ...oadBalancerClassicAtomicOperationTest.java | 2 +- ...CloudSecurityGroupAtomicOperationTest.java | 2 +- ...liCloudServerGroupAtomicOperationTest.java | 79 +++ ...liCloudServerGroupAtomicOperationTest.java | 77 +++ ...liCloudServerGroupAtomicOperationTest.java | 96 ++++ ...ModifyScalingGroupAtomicOperationTest.java | 83 ++++ ...liCloudServerGroupAtomicOperationTest.java | 83 ++++ ...iCloudLaunchConfigAtomicOperationTest.java | 81 +++ ...iCloudLoadBalancerAtomicOperationTest.java | 2 +- ...CloudSecurityGroupAtomicOperationTest.java | 8 +- .../AliCloudLoadBalancerCachingAgentTest.java | 4 +- ...describeLoadBalancerAttributeResponse.json | 2 +- ...describeScalingConfigurationsResponse.json | 1 + .../resources/mock/listenerAttributes.json | 2 +- .../src/test/resources/mock/loadbalancer.json | 6 - .../src/test/resources/mock/scalingGroup.json | 1 + .../test/resources/mock/scalingInstance.json | 1 + .../src/test/resources/mock/vserverGroup.json | 2 +- settings.gradle | 2 +- 84 files changed, 3894 insertions(+), 213 deletions(-) create mode 100644 clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/AliCloudServerGroupNameResolver.java create mode 100644 clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/converters/CopyAliCloudServerGroupAtomicOperationConverter.java create mode 100644 clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/converters/CreateAliCloudServerGroupAtomicOperationConverter.java create mode 100644 clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/converters/DestroyAliCloudServerGroupAtomicOperationConverter.java create mode 100644 clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/converters/DisableAliCloudServerGroupAtomicOperationConverter.java create mode 100644 clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/converters/EnableAliCloudServerGroupAtomicOperationConverter.java create mode 100644 clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/converters/ModifyScalingGroupAtomicOperationConverter.java create mode 100644 clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/converters/ResizeAliCloudServerGroupAtomicOperationConverter.java create mode 100644 clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/converters/UpdateAliCloudLaunchConfigAtomicOperationConverter.java create mode 100644 clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/converters/UpsertAliCloudImageTagsAtomicOperationConverter.java create mode 100644 clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/description/BasicAliCloudDeployDescription.java create mode 100644 clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/description/DestroyAliCloudServerGroupDescription.java create mode 100644 clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/description/DisableAliCloudServerGroupDescription.java rename clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/{security/AliCloudCredentialsConfig.java => deploy/description/EnableAliCloudServerGroupDescription.java} (64%) create mode 100644 clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/description/ModifyScalingGroupDescription.java create mode 100644 clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/description/ResizeAliCloudServerGroupDescription.java create mode 100644 clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/description/UpdateAliCloudLaunchConfigDescription.java create mode 100644 clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/description/UpsertAliCloudImageTagsDescription.java create mode 100644 clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/CopyAliCloudServerGroupAtomicOperation.java create mode 100644 clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/CreateAliCloudServerGroupAtomicOperation.java create mode 100644 clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/DestroyAliCloudServerGroupAtomicOperation.java create mode 100644 clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/DisableAliCloudServerGroupAtomicOperation.java create mode 100644 clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/EnableAliCloudServerGroupAtomicOperation.java create mode 100644 clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/ModifyScalingGroupAtomicOperation.java create mode 100644 clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/ResizeAliCloudServerGroupAtomicOperation.java create mode 100644 clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/UpdateAliCloudLaunchConfigAtomicOperation.java create mode 100644 clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/UpsertAliCloudImageTagsAtomicOperation.java create mode 100644 clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/converters/CreateAliCloudServerGroupAtomicOperationConverterTest.java create mode 100644 clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/converters/DestroyAliCloudServerGroupAtomicOperationConverterTest.java create mode 100644 clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/converters/DisableAliCloudServerGroupAtomicOperationConverterTest.java create mode 100644 clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/converters/EnableAliCloudServerGroupAtomicOperationConverterTest.java create mode 100644 clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/converters/ModifyScalingGroupAtomicOperationConverterTest.java create mode 100644 clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/converters/ResizeAliCloudServerGroupAtomicOperationConverterTest.java create mode 100644 clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/converters/UpdateAliCloudLaunchConfigAtomicOperationConverterTest.java create mode 100644 clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/CreateAliCloudServerGroupAtomicOperationTest.java create mode 100644 clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/DestroyAliCloudServerGroupAtomicOperationTest.java create mode 100644 clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/DisableAliCloudServerGroupAtomicOperationTest.java create mode 100644 clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/EnableAliCloudServerGroupAtomicOperationTest.java create mode 100644 clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/ModifyScalingGroupAtomicOperationTest.java create mode 100644 clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/ResizeAliCloudServerGroupAtomicOperationTest.java create mode 100644 clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/UpdateAliCloudLaunchConfigAtomicOperationTest.java diff --git a/clouddriver-alicloud/README.md b/clouddriver-alicloud/README.md index 40e68ca8eb9..a77f3526a97 100644 --- a/clouddriver-alicloud/README.md +++ b/clouddriver-alicloud/README.md @@ -3,3 +3,25 @@ The clouddriver-alicloud module aims to deploy an application on Alibaba Cloud. It is a work in progress + +### Configuring clouddriver.yml + +```yaml +alicloud: + enabled: true + accounts: + - name: aksk_test + accessKeyId: + accessSecretKey: + regions: + - cn-hangzhou + - cn-shanghai + - name: assumeRole_test + accountId: + assumeRole: role/spinnakermanaged + regions: + - cn-beijing + defaultRegion: cn-hangzhou + accessKeyId: + accessSecretKey: +``` \ No newline at end of file diff --git a/clouddriver-alicloud/clouddriver-alicloud.gradle b/clouddriver-alicloud/clouddriver-alicloud.gradle index f90843a28e8..3e5436c812b 100644 --- a/clouddriver-alicloud/clouddriver-alicloud.gradle +++ b/clouddriver-alicloud/clouddriver-alicloud.gradle @@ -1,8 +1,11 @@ dependencies { implementation project(":cats:cats-core") + implementation project(":clouddriver-api") implementation project(":clouddriver-core") implementation project(":clouddriver-eureka") implementation project(":clouddriver-security") + implementation project(":clouddriver-configserver") + compileOnly "org.projectlombok:lombok" annotationProcessor "org.projectlombok:lombok" @@ -22,10 +25,11 @@ dependencies { implementation 'com.jcraft:jsch.agentproxy.jsch:0.0.9' implementation 'com.jcraft:jsch.agentproxy.connector-factory:0.0.9' implementation 'com.aliyun:aliyun-java-sdk-core:4.4.2' - implementation 'com.aliyun:aliyun-java-sdk-slb:3.2.9' + implementation 'com.aliyun:aliyun-java-sdk-slb:3.3.14' implementation 'com.aliyun:aliyun-java-sdk-vpc:3.0.6' implementation 'com.aliyun:aliyun-java-sdk-ecs:4.16.10' implementation 'com.aliyun:aliyun-java-sdk-ess:2.3.2' + implementation 'com.aliyun:aliyun-java-sdk-sts:3.0.0' implementation "org.springframework.boot:spring-boot-actuator" implementation "org.springframework.boot:spring-boot-starter-web" implementation "org.codehaus.groovy:groovy-all" diff --git a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/cache/Keys.java b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/cache/Keys.java index 643cf3f41b5..3b53e65f853 100644 --- a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/cache/Keys.java +++ b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/cache/Keys.java @@ -53,7 +53,11 @@ public String toString() { public static final String SEPARATOR = ":"; public static String getLoadBalancerKey( - String loadBalancerName, String account, String region, String vpcId) { + String loadBalancerName, + String account, + String region, + String vpcId, + String loadBalancerType) { String key = ID + SEPARATOR @@ -63,10 +67,11 @@ public static String getLoadBalancerKey( + SEPARATOR + region + SEPARATOR - + loadBalancerName; - if (!StringUtils.isEmpty(vpcId)) { - key = key + SEPARATOR + vpcId; - } + + loadBalancerName + + SEPARATOR + + (vpcId == null ? "" : vpcId) + + SEPARATOR + + (loadBalancerType == null ? "" : loadBalancerType); return key; } diff --git a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/AliCloudServerGroupNameResolver.java b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/AliCloudServerGroupNameResolver.java new file mode 100644 index 00000000000..e7dd7504631 --- /dev/null +++ b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/AliCloudServerGroupNameResolver.java @@ -0,0 +1,94 @@ +/* + * Copyright 2022 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.clouddriver.alicloud.deploy; + +import com.netflix.frigga.Names; +import com.netflix.spinnaker.clouddriver.alicloud.AliCloudProvider; +import com.netflix.spinnaker.clouddriver.alicloud.model.AliCloudServerGroup; +import com.netflix.spinnaker.clouddriver.helpers.AbstractServerGroupNameResolver; +import com.netflix.spinnaker.clouddriver.model.Cluster; +import com.netflix.spinnaker.clouddriver.model.ClusterProvider; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.Date; +import java.util.List; +import java.util.stream.Collectors; + +public class AliCloudServerGroupNameResolver extends AbstractServerGroupNameResolver { + + private static final String ALICLOUD_PHASE = "ALICLOUD_DEPLOY"; + + private final String accountName; + private final String region; + private final Collection clusterProviders; + + public AliCloudServerGroupNameResolver( + String accountName, String region, Collection clusterProviders) { + this.accountName = accountName; + this.region = region; + this.clusterProviders = clusterProviders; + } + + @Override + public String getPhase() { + return ALICLOUD_PHASE; + } + + @Override + public String getRegion() { + return region; + } + + @Override + public List getTakenSlots(String clusterName) { + Cluster cluster = getCluster(clusterProviders, accountName, clusterName); + if (cluster == null || cluster.getServerGroups() == null) { + return Collections.emptyList(); + } + ArrayList serverGroups = + (ArrayList) + cluster.getServerGroups().stream() + .filter(it -> region.equals(it.getRegion())) + .collect(Collectors.toList()); + List takenSlots = new ArrayList<>(); + serverGroups.forEach( + it -> + takenSlots.add( + new TakenSlot( + it.getName(), + Names.parseName(it.getName()).getSequence(), + new Date(it.getCreatedTime())))); + return takenSlots; + } + + private static Cluster getCluster( + Collection clusterProviders, String accountName, String clusterName) { + String app = Names.parseName(clusterName).getApp(); + List providers = + clusterProviders.stream() + .filter(it -> AliCloudProvider.ID.equals(it.getCloudProviderId())) + .collect(Collectors.toList()); + if (providers.size() == 1) { + ClusterProvider provider = providers.get(0); + Cluster cluster = provider.getCluster(app, accountName, clusterName); + return cluster; + } else { + return null; + } + } +} diff --git a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/converters/CopyAliCloudServerGroupAtomicOperationConverter.java b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/converters/CopyAliCloudServerGroupAtomicOperationConverter.java new file mode 100644 index 00000000000..2417f561740 --- /dev/null +++ b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/converters/CopyAliCloudServerGroupAtomicOperationConverter.java @@ -0,0 +1,61 @@ +/* + * Copyright 2022 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.clouddriver.alicloud.deploy.converters; + +import com.netflix.spinnaker.clouddriver.alicloud.AliCloudOperation; +import com.netflix.spinnaker.clouddriver.alicloud.common.ClientFactory; +import com.netflix.spinnaker.clouddriver.alicloud.deploy.description.BasicAliCloudDeployDescription; +import com.netflix.spinnaker.clouddriver.alicloud.deploy.ops.CopyAliCloudServerGroupAtomicOperation; +import com.netflix.spinnaker.clouddriver.model.ClusterProvider; +import com.netflix.spinnaker.clouddriver.orchestration.AtomicOperation; +import com.netflix.spinnaker.clouddriver.orchestration.AtomicOperations; +import com.netflix.spinnaker.clouddriver.security.AbstractAtomicOperationsCredentialsSupport; +import java.util.List; +import java.util.Map; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@AliCloudOperation(AtomicOperations.CLONE_SERVER_GROUP) +@Component("copyAliCloudServerGroupDescription") +public class CopyAliCloudServerGroupAtomicOperationConverter + extends AbstractAtomicOperationsCredentialsSupport { + + private final ClientFactory clientFactory; + + private final List clusterProviders; + + @Autowired + public CopyAliCloudServerGroupAtomicOperationConverter( + ClientFactory clientFactory, List clusterProviders) { + this.clientFactory = clientFactory; + this.clusterProviders = clusterProviders; + } + + @Override + public AtomicOperation convertOperation(Map input) { + return new CopyAliCloudServerGroupAtomicOperation( + getObjectMapper(), convertDescription(input), clientFactory, clusterProviders); + } + + @Override + public BasicAliCloudDeployDescription convertDescription(Map input) { + BasicAliCloudDeployDescription description = + getObjectMapper().convertValue(input, BasicAliCloudDeployDescription.class); + description.setCredentials(getCredentialsObject((String) input.get("credentials"))); + return description; + } +} diff --git a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/converters/CreateAliCloudServerGroupAtomicOperationConverter.java b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/converters/CreateAliCloudServerGroupAtomicOperationConverter.java new file mode 100644 index 00000000000..7e389409d74 --- /dev/null +++ b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/converters/CreateAliCloudServerGroupAtomicOperationConverter.java @@ -0,0 +1,102 @@ +/* + * Copyright 2022 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.clouddriver.alicloud.deploy.converters; + +import com.aliyuncs.ess.model.v20140828.CreateScalingGroupRequest; +import com.netflix.spinnaker.clouddriver.alicloud.AliCloudOperation; +import com.netflix.spinnaker.clouddriver.alicloud.common.ClientFactory; +import com.netflix.spinnaker.clouddriver.alicloud.deploy.description.BasicAliCloudDeployDescription; +import com.netflix.spinnaker.clouddriver.alicloud.deploy.ops.CreateAliCloudServerGroupAtomicOperation; +import com.netflix.spinnaker.clouddriver.model.ClusterProvider; +import com.netflix.spinnaker.clouddriver.orchestration.AtomicOperation; +import com.netflix.spinnaker.clouddriver.orchestration.AtomicOperations; +import com.netflix.spinnaker.clouddriver.security.AbstractAtomicOperationsCredentialsSupport; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@AliCloudOperation(AtomicOperations.CREATE_SERVER_GROUP) +@Component("createAliCloudServerGroupDescription") +public class CreateAliCloudServerGroupAtomicOperationConverter + extends AbstractAtomicOperationsCredentialsSupport { + + private final ClientFactory clientFactory; + + private final List clusterProviders; + + @Autowired + public CreateAliCloudServerGroupAtomicOperationConverter( + ClientFactory clientFactory, List clusterProviders) { + this.clientFactory = clientFactory; + this.clusterProviders = clusterProviders; + } + + @Override + public AtomicOperation convertOperation(Map input) { + return new CreateAliCloudServerGroupAtomicOperation( + convertDescription(input), getObjectMapper(), clientFactory, clusterProviders); + } + + @Override + public BasicAliCloudDeployDescription convertDescription(Map input) { + BasicAliCloudDeployDescription description = + getObjectMapper().convertValue(input, BasicAliCloudDeployDescription.class); + + ArrayList vServerGroups = (ArrayList) input.get("vServerGroups"); + if (vServerGroups != null) { + List vServerGroupsNew = new ArrayList<>(); + for (Map map : vServerGroups) { + CreateScalingGroupRequest.VServerGroup vServerGroup = + getObjectMapper().convertValue(map, CreateScalingGroupRequest.VServerGroup.class); + ArrayList vServerGroupAttributes = + (ArrayList) + map.getOrDefault("vserverGroupAttributes", map.get("vServerGroupAttributes")); + List + vServerGroupAttributesList = new ArrayList<>(); + + if (vServerGroupAttributes != null) { + for (Map vServerGroupAttributeMap : vServerGroupAttributes) { + CreateScalingGroupRequest.VServerGroup.VServerGroupAttribute vServerGroupAttribute = + getObjectMapper() + .convertValue( + vServerGroupAttributeMap, + CreateScalingGroupRequest.VServerGroup.VServerGroupAttribute.class); + Object vServerGroupId = + vServerGroupAttributeMap.getOrDefault( + "vserverGroupId", vServerGroupAttributeMap.get("vServerGroupId")); + if (vServerGroupId != null) { + vServerGroupAttribute.setVServerGroupId(vServerGroupId.toString()); + } + vServerGroupAttributesList.add(vServerGroupAttribute); + } + } + + if (vServerGroupAttributesList.stream() + .noneMatch(v -> StringUtils.isBlank(v.getVServerGroupId()))) { + vServerGroup.setVServerGroupAttributes(vServerGroupAttributesList); + vServerGroupsNew.add(vServerGroup); + } + } + description.setVServerGroups(vServerGroupsNew); + } + description.setCredentials(getCredentialsObject((String) input.get("credentials"))); + return description; + } +} diff --git a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/converters/DeleteAliCloudSecurityGroupAtomicOperationConverter.java b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/converters/DeleteAliCloudSecurityGroupAtomicOperationConverter.java index 54d05a754f9..8edc339cd1a 100644 --- a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/converters/DeleteAliCloudSecurityGroupAtomicOperationConverter.java +++ b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/converters/DeleteAliCloudSecurityGroupAtomicOperationConverter.java @@ -5,7 +5,7 @@ * 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 + * 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, diff --git a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/converters/DestroyAliCloudServerGroupAtomicOperationConverter.java b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/converters/DestroyAliCloudServerGroupAtomicOperationConverter.java new file mode 100644 index 00000000000..514b0aaff82 --- /dev/null +++ b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/converters/DestroyAliCloudServerGroupAtomicOperationConverter.java @@ -0,0 +1,54 @@ +/* + * Copyright 2022 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.clouddriver.alicloud.deploy.converters; + +import com.netflix.spinnaker.clouddriver.alicloud.AliCloudOperation; +import com.netflix.spinnaker.clouddriver.alicloud.common.ClientFactory; +import com.netflix.spinnaker.clouddriver.alicloud.deploy.description.DestroyAliCloudServerGroupDescription; +import com.netflix.spinnaker.clouddriver.alicloud.deploy.ops.DestroyAliCloudServerGroupAtomicOperation; +import com.netflix.spinnaker.clouddriver.orchestration.AtomicOperation; +import com.netflix.spinnaker.clouddriver.orchestration.AtomicOperations; +import com.netflix.spinnaker.clouddriver.security.AbstractAtomicOperationsCredentialsSupport; +import java.util.Map; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@AliCloudOperation(AtomicOperations.DESTROY_SERVER_GROUP) +@Component("destroyAliCloudServerGroupDescription") +public class DestroyAliCloudServerGroupAtomicOperationConverter + extends AbstractAtomicOperationsCredentialsSupport { + + private final ClientFactory clientFactory; + + @Autowired + public DestroyAliCloudServerGroupAtomicOperationConverter(ClientFactory clientFactory) { + this.clientFactory = clientFactory; + } + + @Override + public AtomicOperation convertOperation(Map input) { + return new DestroyAliCloudServerGroupAtomicOperation(convertDescription(input), clientFactory); + } + + @Override + public DestroyAliCloudServerGroupDescription convertDescription(Map input) { + DestroyAliCloudServerGroupDescription description = + getObjectMapper().convertValue(input, DestroyAliCloudServerGroupDescription.class); + description.setCredentials(getCredentialsObject((String) input.get("credentials"))); + return description; + } +} diff --git a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/converters/DisableAliCloudServerGroupAtomicOperationConverter.java b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/converters/DisableAliCloudServerGroupAtomicOperationConverter.java new file mode 100644 index 00000000000..dafb42ce712 --- /dev/null +++ b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/converters/DisableAliCloudServerGroupAtomicOperationConverter.java @@ -0,0 +1,54 @@ +/* + * Copyright 2022 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.clouddriver.alicloud.deploy.converters; + +import com.netflix.spinnaker.clouddriver.alicloud.AliCloudOperation; +import com.netflix.spinnaker.clouddriver.alicloud.common.ClientFactory; +import com.netflix.spinnaker.clouddriver.alicloud.deploy.description.DisableAliCloudServerGroupDescription; +import com.netflix.spinnaker.clouddriver.alicloud.deploy.ops.DisableAliCloudServerGroupAtomicOperation; +import com.netflix.spinnaker.clouddriver.orchestration.AtomicOperation; +import com.netflix.spinnaker.clouddriver.orchestration.AtomicOperations; +import com.netflix.spinnaker.clouddriver.security.AbstractAtomicOperationsCredentialsSupport; +import java.util.Map; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@AliCloudOperation(AtomicOperations.DISABLE_SERVER_GROUP) +@Component("disableAliCloudServerGroupDescription") +public class DisableAliCloudServerGroupAtomicOperationConverter + extends AbstractAtomicOperationsCredentialsSupport { + + private final ClientFactory clientFactory; + + @Autowired + public DisableAliCloudServerGroupAtomicOperationConverter(ClientFactory clientFactory) { + this.clientFactory = clientFactory; + } + + @Override + public AtomicOperation convertOperation(Map input) { + return new DisableAliCloudServerGroupAtomicOperation(convertDescription(input), clientFactory); + } + + @Override + public DisableAliCloudServerGroupDescription convertDescription(Map input) { + DisableAliCloudServerGroupDescription description = + getObjectMapper().convertValue(input, DisableAliCloudServerGroupDescription.class); + description.setCredentials(getCredentialsObject((String) input.get("credentials"))); + return description; + } +} diff --git a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/converters/EnableAliCloudServerGroupAtomicOperationConverter.java b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/converters/EnableAliCloudServerGroupAtomicOperationConverter.java new file mode 100644 index 00000000000..af4efc618e3 --- /dev/null +++ b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/converters/EnableAliCloudServerGroupAtomicOperationConverter.java @@ -0,0 +1,54 @@ +/* + * Copyright 2022 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.clouddriver.alicloud.deploy.converters; + +import com.netflix.spinnaker.clouddriver.alicloud.AliCloudOperation; +import com.netflix.spinnaker.clouddriver.alicloud.common.ClientFactory; +import com.netflix.spinnaker.clouddriver.alicloud.deploy.description.EnableAliCloudServerGroupDescription; +import com.netflix.spinnaker.clouddriver.alicloud.deploy.ops.EnableAliCloudServerGroupAtomicOperation; +import com.netflix.spinnaker.clouddriver.orchestration.AtomicOperation; +import com.netflix.spinnaker.clouddriver.orchestration.AtomicOperations; +import com.netflix.spinnaker.clouddriver.security.AbstractAtomicOperationsCredentialsSupport; +import java.util.Map; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@AliCloudOperation(AtomicOperations.ENABLE_SERVER_GROUP) +@Component("enableAliCloudServerGroupDescription") +public class EnableAliCloudServerGroupAtomicOperationConverter + extends AbstractAtomicOperationsCredentialsSupport { + + private final ClientFactory clientFactory; + + @Autowired + public EnableAliCloudServerGroupAtomicOperationConverter(ClientFactory clientFactory) { + this.clientFactory = clientFactory; + } + + @Override + public AtomicOperation convertOperation(Map input) { + return new EnableAliCloudServerGroupAtomicOperation(convertDescription(input), clientFactory); + } + + @Override + public EnableAliCloudServerGroupDescription convertDescription(Map input) { + EnableAliCloudServerGroupDescription description = + getObjectMapper().convertValue(input, EnableAliCloudServerGroupDescription.class); + description.setCredentials(getCredentialsObject((String) input.get("credentials"))); + return description; + } +} diff --git a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/converters/ModifyScalingGroupAtomicOperationConverter.java b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/converters/ModifyScalingGroupAtomicOperationConverter.java new file mode 100644 index 00000000000..04e3b7e2a27 --- /dev/null +++ b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/converters/ModifyScalingGroupAtomicOperationConverter.java @@ -0,0 +1,52 @@ +/* + * Copyright 2022 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.clouddriver.alicloud.deploy.converters; + +import com.netflix.spinnaker.clouddriver.alicloud.common.ClientFactory; +import com.netflix.spinnaker.clouddriver.alicloud.deploy.description.ModifyScalingGroupDescription; +import com.netflix.spinnaker.clouddriver.alicloud.deploy.ops.ModifyScalingGroupAtomicOperation; +import com.netflix.spinnaker.clouddriver.orchestration.AtomicOperation; +import com.netflix.spinnaker.clouddriver.security.AbstractAtomicOperationsCredentialsSupport; +import java.util.Map; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Component("modifyScalingGroupDescription") +public class ModifyScalingGroupAtomicOperationConverter + extends AbstractAtomicOperationsCredentialsSupport { + + private final ClientFactory clientFactory; + + @Autowired + public ModifyScalingGroupAtomicOperationConverter(ClientFactory clientFactory) { + this.clientFactory = clientFactory; + } + + @Override + public AtomicOperation convertOperation(Map input) { + return new ModifyScalingGroupAtomicOperation( + convertDescription(input), getObjectMapper(), clientFactory); + } + + @Override + public ModifyScalingGroupDescription convertDescription(Map input) { + ModifyScalingGroupDescription description = + getObjectMapper().convertValue(input, ModifyScalingGroupDescription.class); + description.setCredentials(getCredentialsObject((String) input.get("credentials"))); + return description; + } +} diff --git a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/converters/ResizeAliCloudServerGroupAtomicOperationConverter.java b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/converters/ResizeAliCloudServerGroupAtomicOperationConverter.java new file mode 100644 index 00000000000..11d8a09b0e1 --- /dev/null +++ b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/converters/ResizeAliCloudServerGroupAtomicOperationConverter.java @@ -0,0 +1,54 @@ +/* + * Copyright 2022 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.clouddriver.alicloud.deploy.converters; + +import com.netflix.spinnaker.clouddriver.alicloud.AliCloudOperation; +import com.netflix.spinnaker.clouddriver.alicloud.common.ClientFactory; +import com.netflix.spinnaker.clouddriver.alicloud.deploy.description.ResizeAliCloudServerGroupDescription; +import com.netflix.spinnaker.clouddriver.alicloud.deploy.ops.ResizeAliCloudServerGroupAtomicOperation; +import com.netflix.spinnaker.clouddriver.orchestration.AtomicOperation; +import com.netflix.spinnaker.clouddriver.orchestration.AtomicOperations; +import com.netflix.spinnaker.clouddriver.security.AbstractAtomicOperationsCredentialsSupport; +import java.util.Map; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@AliCloudOperation(AtomicOperations.RESIZE_SERVER_GROUP) +@Component("resizeAliCloudServerGroupDescription") +public class ResizeAliCloudServerGroupAtomicOperationConverter + extends AbstractAtomicOperationsCredentialsSupport { + + private final ClientFactory clientFactory; + + @Autowired + public ResizeAliCloudServerGroupAtomicOperationConverter(ClientFactory clientFactory) { + this.clientFactory = clientFactory; + } + + @Override + public AtomicOperation convertOperation(Map input) { + return new ResizeAliCloudServerGroupAtomicOperation(convertDescription(input), clientFactory); + } + + @Override + public ResizeAliCloudServerGroupDescription convertDescription(Map input) { + ResizeAliCloudServerGroupDescription description = + getObjectMapper().convertValue(input, ResizeAliCloudServerGroupDescription.class); + description.setCredentials(getCredentialsObject((String) input.get("credentials"))); + return description; + } +} diff --git a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/converters/UpdateAliCloudLaunchConfigAtomicOperationConverter.java b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/converters/UpdateAliCloudLaunchConfigAtomicOperationConverter.java new file mode 100644 index 00000000000..233ee27d473 --- /dev/null +++ b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/converters/UpdateAliCloudLaunchConfigAtomicOperationConverter.java @@ -0,0 +1,55 @@ +/* + * Copyright 2022 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.clouddriver.alicloud.deploy.converters; + +import com.netflix.spinnaker.clouddriver.alicloud.AliCloudOperation; +import com.netflix.spinnaker.clouddriver.alicloud.common.ClientFactory; +import com.netflix.spinnaker.clouddriver.alicloud.deploy.description.UpdateAliCloudLaunchConfigDescription; +import com.netflix.spinnaker.clouddriver.alicloud.deploy.ops.UpdateAliCloudLaunchConfigAtomicOperation; +import com.netflix.spinnaker.clouddriver.orchestration.AtomicOperation; +import com.netflix.spinnaker.clouddriver.orchestration.AtomicOperations; +import com.netflix.spinnaker.clouddriver.security.AbstractAtomicOperationsCredentialsSupport; +import java.util.Map; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@AliCloudOperation(AtomicOperations.UPDATE_LAUNCH_CONFIG) +@Component("updateAliCloudLaunchConfigDescription") +public class UpdateAliCloudLaunchConfigAtomicOperationConverter + extends AbstractAtomicOperationsCredentialsSupport { + + private final ClientFactory clientFactory; + + @Autowired + public UpdateAliCloudLaunchConfigAtomicOperationConverter(ClientFactory clientFactory) { + this.clientFactory = clientFactory; + } + + @Override + public AtomicOperation convertOperation(Map input) { + return new UpdateAliCloudLaunchConfigAtomicOperation( + convertDescription(input), getObjectMapper(), clientFactory); + } + + @Override + public UpdateAliCloudLaunchConfigDescription convertDescription(Map input) { + UpdateAliCloudLaunchConfigDescription description = + getObjectMapper().convertValue(input, UpdateAliCloudLaunchConfigDescription.class); + description.setCredentials(getCredentialsObject((String) input.get("credentials"))); + return description; + } +} diff --git a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/converters/UpsertAliCloudImageTagsAtomicOperationConverter.java b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/converters/UpsertAliCloudImageTagsAtomicOperationConverter.java new file mode 100644 index 00000000000..595cf1151c9 --- /dev/null +++ b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/converters/UpsertAliCloudImageTagsAtomicOperationConverter.java @@ -0,0 +1,55 @@ +/* + * Copyright 2022 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.clouddriver.alicloud.deploy.converters; + +import com.netflix.spinnaker.clouddriver.alicloud.AliCloudOperation; +import com.netflix.spinnaker.clouddriver.alicloud.common.ClientFactory; +import com.netflix.spinnaker.clouddriver.alicloud.deploy.description.UpsertAliCloudImageTagsDescription; +import com.netflix.spinnaker.clouddriver.alicloud.deploy.ops.UpsertAliCloudImageTagsAtomicOperation; +import com.netflix.spinnaker.clouddriver.orchestration.AtomicOperation; +import com.netflix.spinnaker.clouddriver.orchestration.AtomicOperations; +import com.netflix.spinnaker.clouddriver.security.AbstractAtomicOperationsCredentialsSupport; +import java.util.Map; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@AliCloudOperation(AtomicOperations.UPSERT_IMAGE_TAGS) +@Component("upsertAliCloudImageTagsDescription") +public class UpsertAliCloudImageTagsAtomicOperationConverter + extends AbstractAtomicOperationsCredentialsSupport { + + private final ClientFactory clientFactory; + + @Autowired + public UpsertAliCloudImageTagsAtomicOperationConverter(ClientFactory clientFactory) { + this.clientFactory = clientFactory; + } + + @Override + public AtomicOperation convertOperation(Map input) { + return new UpsertAliCloudImageTagsAtomicOperation( + convertDescription(input), getObjectMapper(), clientFactory); + } + + @Override + public UpsertAliCloudImageTagsDescription convertDescription(Map input) { + UpsertAliCloudImageTagsDescription converted = + getObjectMapper().convertValue(input, UpsertAliCloudImageTagsDescription.class); + converted.setCredentials(getCredentialsObject((String) input.get("credentials"))); + return converted; + } +} diff --git a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/converters/UpsertAliCloudSecurityGroupAtomicOperationConverter.java b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/converters/UpsertAliCloudSecurityGroupAtomicOperationConverter.java index 42d55be8b5f..66cf8ddcda3 100644 --- a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/converters/UpsertAliCloudSecurityGroupAtomicOperationConverter.java +++ b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/converters/UpsertAliCloudSecurityGroupAtomicOperationConverter.java @@ -5,7 +5,7 @@ * 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 + * 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, diff --git a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/description/BaseAliCloudDescription.java b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/description/BaseAliCloudDescription.java index d2c06683948..015bd1b5055 100644 --- a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/description/BaseAliCloudDescription.java +++ b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/description/BaseAliCloudDescription.java @@ -18,10 +18,11 @@ import com.fasterxml.jackson.annotation.JsonIgnore; import com.netflix.spinnaker.clouddriver.alicloud.security.AliCloudCredentials; +import com.netflix.spinnaker.clouddriver.orchestration.OperationDescription; import lombok.Data; @Data -public class BaseAliCloudDescription { +public class BaseAliCloudDescription implements OperationDescription { @JsonIgnore private AliCloudCredentials credentials; diff --git a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/description/BasicAliCloudDeployDescription.java b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/description/BasicAliCloudDeployDescription.java new file mode 100644 index 00000000000..073ed71a6b5 --- /dev/null +++ b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/description/BasicAliCloudDeployDescription.java @@ -0,0 +1,85 @@ +/* + * Copyright 2022 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.clouddriver.alicloud.deploy.description; + +import com.aliyuncs.ess.model.v20140828.CreateScalingConfigurationRequest; +import com.aliyuncs.ess.model.v20140828.CreateScalingGroupRequest.LifecycleHook; +import com.aliyuncs.ess.model.v20140828.CreateScalingGroupRequest.VServerGroup; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.netflix.spinnaker.clouddriver.deploy.DeployDescription; +import com.netflix.spinnaker.clouddriver.deploy.DeploymentResult.Deployment.Capacity; +import com.netflix.spinnaker.clouddriver.security.resources.ApplicationNameable; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import lombok.Data; +import lombok.EqualsAndHashCode; + +@EqualsAndHashCode(callSuper = true) +@Data +public class BasicAliCloudDeployDescription extends BaseAliCloudDescription + implements DeployDescription, ApplicationNameable { + + private String multiAZPolicy; + private String dBInstanceIds; + private String launchTemplateId; + private String loadBalancerIds; + private String healthCheckType; + private String resourceOwnerAccount; + private String scalingGroupName; + private String clientToken; + private String ownerAccount; + private Integer minSize; + private Long ownerId; + private String launchTemplateVersion; + private String scalingPolicy; + private Integer maxSize; + private List lifecycleHooks; + private Integer defaultCooldown; + private String removalPolicy1; + private List vServerGroups; + private String removalPolicy2; + private String scalingGroupId; + private String application; + private String stack; + private String freeFormDetails; + private List scalingConfigurations; + + @JsonProperty("vSwitchId") + private String vSwitchId; + + @JsonProperty("vSwitchIds") + private List vSwitchIds; + + Capacity capacity = new Capacity(); + Source source = new Source(); + + @Override + public Collection getApplications() { + List applications = new ArrayList<>(); + applications.add(application); + return applications; + } + + @Data + public static class Source { + private String account; + private String region; + private String asgName; + private Boolean useSourceCapacity; + } +} diff --git a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/description/DeleteAliCloudSecurityGroupDescription.java b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/description/DeleteAliCloudSecurityGroupDescription.java index 2d4661418cf..4e74d97a15e 100644 --- a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/description/DeleteAliCloudSecurityGroupDescription.java +++ b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/description/DeleteAliCloudSecurityGroupDescription.java @@ -18,7 +18,9 @@ import java.util.List; import lombok.Data; +import lombok.EqualsAndHashCode; +@EqualsAndHashCode(callSuper = true) @Data public class DeleteAliCloudSecurityGroupDescription extends BaseAliCloudDescription { diff --git a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/description/DestroyAliCloudServerGroupDescription.java b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/description/DestroyAliCloudServerGroupDescription.java new file mode 100644 index 00000000000..a94260dc5f7 --- /dev/null +++ b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/description/DestroyAliCloudServerGroupDescription.java @@ -0,0 +1,29 @@ +/* + * Copyright 2022 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.clouddriver.alicloud.deploy.description; + +import lombok.Data; +import lombok.EqualsAndHashCode; + +@EqualsAndHashCode(callSuper = true) +@Data +public class DestroyAliCloudServerGroupDescription extends BaseAliCloudDescription { + + private String scalingGroupName; + + private String serverGroupName; +} diff --git a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/description/DisableAliCloudServerGroupDescription.java b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/description/DisableAliCloudServerGroupDescription.java new file mode 100644 index 00000000000..c1ee4c323ec --- /dev/null +++ b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/description/DisableAliCloudServerGroupDescription.java @@ -0,0 +1,29 @@ +/* + * Copyright 2022 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.clouddriver.alicloud.deploy.description; + +import lombok.Data; +import lombok.EqualsAndHashCode; + +@EqualsAndHashCode(callSuper = true) +@Data +public class DisableAliCloudServerGroupDescription extends BaseAliCloudDescription { + + private String scalingGroupName; + + private String serverGroupName; +} diff --git a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/security/AliCloudCredentialsConfig.java b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/description/EnableAliCloudServerGroupDescription.java similarity index 64% rename from clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/security/AliCloudCredentialsConfig.java rename to clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/description/EnableAliCloudServerGroupDescription.java index 28e95885d11..5bfe364d487 100644 --- a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/security/AliCloudCredentialsConfig.java +++ b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/description/EnableAliCloudServerGroupDescription.java @@ -1,5 +1,5 @@ /* - * Copyright 2019 Alibaba Group. + * Copyright 2022 Alibaba Group. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,19 +13,17 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.netflix.spinnaker.clouddriver.alicloud.security; -import java.util.List; +package com.netflix.spinnaker.clouddriver.alicloud.deploy.description; + import lombok.Data; +import lombok.EqualsAndHashCode; +@EqualsAndHashCode(callSuper = true) @Data -public class AliCloudCredentialsConfig { +public class EnableAliCloudServerGroupDescription extends BaseAliCloudDescription { - List accounts; + private String scalingGroupName; - @Data - public static class Account { - private String name; - private String aliAccount; - } + private String serverGroupName; } diff --git a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/description/ModifyScalingGroupDescription.java b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/description/ModifyScalingGroupDescription.java new file mode 100644 index 00000000000..3818d59be8b --- /dev/null +++ b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/description/ModifyScalingGroupDescription.java @@ -0,0 +1,72 @@ +/* + * Copyright 2022 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.clouddriver.alicloud.deploy.description; + +import com.fasterxml.jackson.annotation.JsonProperty; +import java.util.List; +import lombok.Data; +import lombok.EqualsAndHashCode; + +@EqualsAndHashCode(callSuper = true) +@Data +public class ModifyScalingGroupDescription extends BaseAliCloudDescription { + + private String scalingGroupName; + + private String serverGroupName; + + private Long resourceOwnerId; + + private String healthCheckType; + + private String launchTemplateId; + + private String resourceOwnerAccount; + + private String scalingGroupId; + + @JsonProperty("vSwitchIds") + private List vSwitchIds; + + private String ownerAccount; + + private String activeScalingConfigurationId; + + private Integer minSize; + + private Long ownerId; + + private String launchTemplateVersion; + + private Integer maxSize; + + private Integer defaultCooldown; + + private String removalPolicy1; + + private String removalPolicy2; + + private List scalingGroups; + + @Data + public static class ScalingGroup { + + private String scalingGroupName; + + private String region; + } +} diff --git a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/description/ResizeAliCloudServerGroupDescription.java b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/description/ResizeAliCloudServerGroupDescription.java new file mode 100644 index 00000000000..b003f4c5048 --- /dev/null +++ b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/description/ResizeAliCloudServerGroupDescription.java @@ -0,0 +1,38 @@ +/* + * Copyright 2022 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.clouddriver.alicloud.deploy.description; + +import java.util.LinkedHashMap; +import lombok.Data; +import lombok.EqualsAndHashCode; + +@EqualsAndHashCode(callSuper = true) +@Data +public class ResizeAliCloudServerGroupDescription extends BaseAliCloudDescription { + + private String scalingGroupName; + + private String serverGroupName; + + private Integer minSize; + + private Integer maxSize; + + private Integer scaleNum; + + private LinkedHashMap capacity; +} diff --git a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/description/UpdateAliCloudLaunchConfigDescription.java b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/description/UpdateAliCloudLaunchConfigDescription.java new file mode 100644 index 00000000000..dad981d8cd7 --- /dev/null +++ b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/description/UpdateAliCloudLaunchConfigDescription.java @@ -0,0 +1,126 @@ +/* + * Copyright 2022 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.clouddriver.alicloud.deploy.description; + +import java.util.List; +import lombok.Data; +import lombok.EqualsAndHashCode; + +@EqualsAndHashCode(callSuper = true) +@Data +public class UpdateAliCloudLaunchConfigDescription extends BaseAliCloudDescription { + + private String imageId; + + private Integer memory; + + private String ioOptimized; + + private List instanceTypes; + + private Integer internetMaxBandwidthOut; + + private String securityGroupId; + + private String keyPairName; + + private List spotPriceLimits; + + private String systemDiskCategory; + + private String userData; + + private String resourceGroupId; + + private String hostName; + + private Boolean passwordInherit; + + private String imageName; + + private Boolean override; + + private String deploymentSetId; + + private String resourceOwnerAccount; + + private String ownerAccount; + + private Integer cpu; + + private String systemDiskDiskName; + + private String ramRoleName; + + private Long ownerId; + + private List dataDisks; + + private String scalingConfigurationName; + + private String tags; + + private String scalingConfigurationId; + + private String spotStrategy; + + private String instanceName; + + private Integer loadBalancerWeight; + + private Integer systemDiskSize; + + private String internetChargeType; + + private String systemDiskDescription; + + private String scalingGroupName; + + private String serverGroupName; + + private List securityGroups; + + @Data + public static class SpotPriceLimit { + + private String instanceType; + + private Float priceLimit; + } + + @Data + public static class DataDisk { + + private String diskName; + + private String snapshotId; + + private Integer size; + + private String encrypted; + + private String description; + + private String category; + + private String kMSKeyId; + + private String device; + + private Boolean deleteWithInstance; + } +} diff --git a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/description/UpsertAliCloudImageTagsDescription.java b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/description/UpsertAliCloudImageTagsDescription.java new file mode 100644 index 00000000000..9c8b7812efb --- /dev/null +++ b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/description/UpsertAliCloudImageTagsDescription.java @@ -0,0 +1,29 @@ +/* + * Copyright 2022 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.clouddriver.alicloud.deploy.description; + +import java.util.Map; +import lombok.Data; +import lombok.EqualsAndHashCode; + +@EqualsAndHashCode(callSuper = true) +@Data +public class UpsertAliCloudImageTagsDescription extends BaseAliCloudDescription { + private String imageName; + private Map tags; + private String accountName; +} diff --git a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/description/UpsertAliCloudLoadBalancerDescription.java b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/description/UpsertAliCloudLoadBalancerDescription.java index 6bac76d1da9..cbe5097a792 100644 --- a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/description/UpsertAliCloudLoadBalancerDescription.java +++ b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/description/UpsertAliCloudLoadBalancerDescription.java @@ -16,19 +16,15 @@ package com.netflix.spinnaker.clouddriver.alicloud.deploy.description; -import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonProperty; import com.netflix.spinnaker.clouddriver.alicloud.model.Listener; -import com.netflix.spinnaker.clouddriver.alicloud.security.AliCloudCredentials; import java.util.List; import lombok.Data; +import lombok.EqualsAndHashCode; +@EqualsAndHashCode(callSuper = true) @Data -public class UpsertAliCloudLoadBalancerDescription { - - @JsonIgnore private AliCloudCredentials credentials; - - private String region; +public class UpsertAliCloudLoadBalancerDescription extends BaseAliCloudDescription { private List listeners; diff --git a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/description/UpsertAliCloudSecurityGroupDescription.java b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/description/UpsertAliCloudSecurityGroupDescription.java index 1369291413f..477946ac63d 100644 --- a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/description/UpsertAliCloudSecurityGroupDescription.java +++ b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/description/UpsertAliCloudSecurityGroupDescription.java @@ -20,7 +20,9 @@ import com.aliyuncs.ecs.model.v20140526.AuthorizeSecurityGroupRequest; import java.util.List; import lombok.Data; +import lombok.EqualsAndHashCode; +@EqualsAndHashCode(callSuper = true) @Data public class UpsertAliCloudSecurityGroupDescription extends BaseAliCloudDescription { diff --git a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/CopyAliCloudServerGroupAtomicOperation.java b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/CopyAliCloudServerGroupAtomicOperation.java new file mode 100644 index 00000000000..41b4dec6aa2 --- /dev/null +++ b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/CopyAliCloudServerGroupAtomicOperation.java @@ -0,0 +1,470 @@ +/* + * Copyright 2022 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.clouddriver.alicloud.deploy.ops; + +import com.aliyuncs.IAcsClient; +import com.aliyuncs.ess.model.v20140828.CreateScalingConfigurationRequest; +import com.aliyuncs.ess.model.v20140828.CreateScalingConfigurationRequest.SpotPriceLimit; +import com.aliyuncs.ess.model.v20140828.CreateScalingConfigurationResponse; +import com.aliyuncs.ess.model.v20140828.CreateScalingGroupRequest; +import com.aliyuncs.ess.model.v20140828.CreateScalingGroupResponse; +import com.aliyuncs.ess.model.v20140828.DescribeScalingConfigurationsRequest; +import com.aliyuncs.ess.model.v20140828.DescribeScalingConfigurationsResponse; +import com.aliyuncs.ess.model.v20140828.DescribeScalingConfigurationsResponse.ScalingConfiguration; +import com.aliyuncs.ess.model.v20140828.DescribeScalingConfigurationsResponse.ScalingConfiguration.DataDisk; +import com.aliyuncs.ess.model.v20140828.DescribeScalingConfigurationsResponse.ScalingConfiguration.SpotPriceModel; +import com.aliyuncs.ess.model.v20140828.DescribeScalingConfigurationsResponse.ScalingConfiguration.Tag; +import com.aliyuncs.ess.model.v20140828.DescribeScalingGroupsRequest; +import com.aliyuncs.ess.model.v20140828.DescribeScalingGroupsResponse; +import com.aliyuncs.ess.model.v20140828.DescribeScalingGroupsResponse.ScalingGroup; +import com.aliyuncs.ess.model.v20140828.DescribeScalingGroupsResponse.ScalingGroup.VServerGroup; +import com.aliyuncs.ess.model.v20140828.DescribeScalingGroupsResponse.ScalingGroup.VServerGroup.VServerGroupAttribute; +import com.aliyuncs.ess.model.v20140828.EnableScalingGroupRequest; +import com.aliyuncs.exceptions.ClientException; +import com.aliyuncs.exceptions.ServerException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.gson.Gson; +import com.netflix.spinnaker.clouddriver.alicloud.AliCloudProvider; +import com.netflix.spinnaker.clouddriver.alicloud.common.ClientFactory; +import com.netflix.spinnaker.clouddriver.alicloud.deploy.AliCloudServerGroupNameResolver; +import com.netflix.spinnaker.clouddriver.alicloud.deploy.description.BasicAliCloudDeployDescription; +import com.netflix.spinnaker.clouddriver.alicloud.exception.AliCloudException; +import com.netflix.spinnaker.clouddriver.deploy.DeploymentResult; +import com.netflix.spinnaker.clouddriver.deploy.DeploymentResult.Deployment; +import com.netflix.spinnaker.clouddriver.model.ClusterProvider; +import com.netflix.spinnaker.clouddriver.orchestration.AtomicOperation; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; + +@Slf4j +public class CopyAliCloudServerGroupAtomicOperation implements AtomicOperation { + + private final List clusterProviders; + + private final ObjectMapper objectMapper; + + private final BasicAliCloudDeployDescription description; + + private final ClientFactory clientFactory; + + static final Gson gson = new Gson(); + + public CopyAliCloudServerGroupAtomicOperation( + ObjectMapper objectMapper, + BasicAliCloudDeployDescription description, + ClientFactory clientFactory, + List clusterProviders) { + this.objectMapper = objectMapper; + this.description = description; + this.clientFactory = clientFactory; + this.clusterProviders = clusterProviders; + } + + @Override + public DeploymentResult operate(List priorOutputs) { + DeploymentResult result = new DeploymentResult(); + // create scaling group + IAcsClient client = + clientFactory.createClient( + description.getRegion(), description.getCredentials().getCredentialsProvider()); + AliCloudServerGroupNameResolver resolver = + new AliCloudServerGroupNameResolver( + description.getCredentials().getName(), description.getRegion(), clusterProviders); + String serverGroupName = + resolver.resolveNextServerGroupName( + description.getApplication(), + description.getStack(), + description.getFreeFormDetails(), + false); + description.setScalingGroupName(serverGroupName); + String asgName = description.getSource().getAsgName(); + DescribeScalingGroupsRequest request = new DescribeScalingGroupsRequest(); + request.setScalingGroupName(asgName); + DescribeScalingGroupsResponse response; + try { + response = client.getAcsResponse(request); + if (response.getScalingGroups().size() == 0) { + throw new AliCloudException("Old server group is does not exist"); + } + ScalingGroup scalingGroup = response.getScalingGroups().get(0); + + CreateScalingGroupRequest createScalingGroupRequest = + buildScalingGroupData(description, scalingGroup); + + CreateScalingGroupResponse createScalingGroupResponse = + client.getAcsResponse(createScalingGroupRequest); + String scalingGroupId = createScalingGroupResponse.getScalingGroupId(); + description.setScalingGroupId(scalingGroupId); + DescribeScalingConfigurationsRequest scalingConfigurationsRequest = + new DescribeScalingConfigurationsRequest(); + scalingConfigurationsRequest.setScalingGroupId(scalingGroup.getScalingGroupId()); + scalingConfigurationsRequest.setScalingConfigurationId1( + scalingGroup.getActiveScalingConfigurationId()); + DescribeScalingConfigurationsResponse scalingConfiguration = + client.getAcsResponse(scalingConfigurationsRequest); + + CreateScalingConfigurationRequest createScalingConfigurationRequest = + buildScalingConfigurationData( + description.getScalingConfigurations(), scalingConfiguration); + + createScalingConfigurationRequest.setScalingGroupId(description.getScalingGroupId()); + CreateScalingConfigurationResponse configurationResponse = + client.getAcsResponse(createScalingConfigurationRequest); + + EnableScalingGroupRequest enableScalingGroupRequest = new EnableScalingGroupRequest(); + enableScalingGroupRequest.setScalingGroupId(description.getScalingGroupId()); + enableScalingGroupRequest.setActiveScalingConfigurationId( + configurationResponse.getScalingConfigurationId()); + client.getAcsResponse(enableScalingGroupRequest); + + } catch (ServerException e) { + log.info(e.getMessage()); + throw new AliCloudException(e.getMessage()); + } catch (ClientException e) { + log.info(e.getMessage()); + throw new AliCloudException(e.getMessage()); + } + + buildResult(description, result); + return result; + } + + private CreateScalingGroupRequest buildScalingGroupData( + BasicAliCloudDeployDescription description, ScalingGroup scalingGroup) { + CreateScalingGroupRequest request = new CreateScalingGroupRequest(); + request.setMaxSize( + description.getMaxSize() != null + ? description.getMaxSize() + : (description.getCapacity().getMax() != null + ? description.getCapacity().getMax() + : scalingGroup.getMaxSize())); + request.setMinSize( + description.getMinSize() != null + ? description.getMinSize() + : (description.getCapacity().getMin() != null + ? description.getCapacity().getMin() + : scalingGroup.getMinSize())); + request.setClientToken(description.getClientToken()); + if (description.getDBInstanceIds() != null) { + request.setDBInstanceIds(description.getDBInstanceIds()); + } else { + if (scalingGroup.getDBInstanceIds().size() > 0) { + request.setDBInstanceIds(gson.toJson(scalingGroup.getDBInstanceIds())); + } + } + request.setDefaultCooldown( + description.getDefaultCooldown() != null + ? description.getDefaultCooldown() + : scalingGroup.getDefaultCooldown()); + request.setHealthCheckType( + description.getHealthCheckType() != null + ? description.getHealthCheckType() + : scalingGroup.getHealthCheckType()); + request.setLaunchTemplateId( + description.getLaunchTemplateId() != null + ? description.getLaunchTemplateId() + : scalingGroup.getLaunchTemplateId()); + request.setLaunchTemplateVersion( + description.getLaunchTemplateVersion() != null + ? description.getLaunchTemplateVersion() + : scalingGroup.getLaunchTemplateVersion()); + if (description.getLifecycleHooks() != null && description.getLifecycleHooks().size() > 0) { + request.setLifecycleHooks(description.getLifecycleHooks()); + } + if (StringUtils.isNotEmpty(description.getLoadBalancerIds())) { + request.setLoadBalancerIds(description.getLoadBalancerIds()); + } else { + if (scalingGroup.getLoadBalancerIds().size() > 0) { + request.setLoadBalancerIds(gson.toJson(scalingGroup)); + } + } + request.setMultiAZPolicy( + description.getMultiAZPolicy() != null + ? description.getMultiAZPolicy() + : scalingGroup.getMultiAZPolicy()); + if (StringUtils.isNotEmpty(description.getRemovalPolicy1())) { + request.setRemovalPolicy1(description.getRemovalPolicy1()); + } else { + if (scalingGroup.getRemovalPolicies().size() >= 1) { + request.setRemovalPolicy1(scalingGroup.getRemovalPolicies().get(0)); + } + } + if (StringUtils.isNotEmpty(description.getRemovalPolicy2())) { + request.setRemovalPolicy2(description.getRemovalPolicy2()); + } else { + if (scalingGroup.getRemovalPolicies().size() == 2) { + request.setRemovalPolicy2(scalingGroup.getRemovalPolicies().get(1)); + } + } + request.setScalingGroupName( + description.getScalingGroupName() != null + ? description.getScalingGroupName() + : scalingGroup.getScalingGroupName()); + request.setScalingPolicy( + description.getScalingPolicy() != null + ? description.getScalingPolicy() + : scalingGroup.getScalingPolicy()); + if (description.getVServerGroups() != null && description.getVServerGroups().size() > 0) { + request.setVServerGroups(description.getVServerGroups()); + } else { + if (scalingGroup.getVServerGroups().size() > 0) { + List serverGroups = new ArrayList<>(); + for (VServerGroup vServerGroup : scalingGroup.getVServerGroups()) { + CreateScalingGroupRequest.VServerGroup group = + new CreateScalingGroupRequest.VServerGroup(); + group.setLoadBalancerId(vServerGroup.getLoadBalancerId()); + List + vServerGroupAttributes = new ArrayList<>(); + for (VServerGroupAttribute vServerGroupAttribute : + vServerGroup.getVServerGroupAttributes()) { + CreateScalingGroupRequest.VServerGroup.VServerGroupAttribute attribute = + new CreateScalingGroupRequest.VServerGroup.VServerGroupAttribute(); + attribute.setPort(vServerGroupAttribute.getPort()); + attribute.setVServerGroupId(vServerGroupAttribute.getVServerGroupId()); + attribute.setWeight(vServerGroupAttribute.getWeight()); + vServerGroupAttributes.add(attribute); + } + group.setVServerGroupAttributes(vServerGroupAttributes); + serverGroups.add(group); + } + request.setVServerGroups(serverGroups); + } + } + request.setVSwitchId( + description.getVSwitchId() != null + ? description.getVSwitchId() + : scalingGroup.getVSwitchId()); + request.setVSwitchIds( + description.getVSwitchIds() != null + ? description.getVSwitchIds() + : scalingGroup.getVSwitchIds()); + + return request; + } + + private CreateScalingConfigurationRequest buildScalingConfigurationData( + List scalingConfigurations, + DescribeScalingConfigurationsResponse describeScalingConfigurationsResponse) { + CreateScalingConfigurationRequest createScalingConfigurationRequest = + new CreateScalingConfigurationRequest(); + CreateScalingConfigurationRequest scalingConfigurationNew = null; + if (scalingConfigurations != null && scalingConfigurations.size() > 0) { + scalingConfigurationNew = scalingConfigurations.get(0); + } else { + scalingConfigurationNew = new CreateScalingConfigurationRequest(); + } + ScalingConfiguration scalingConfigurationOld = null; + if (describeScalingConfigurationsResponse.getScalingConfigurations().size() > 0) { + scalingConfigurationOld = + describeScalingConfigurationsResponse.getScalingConfigurations().get(0); + } else { + scalingConfigurationOld = new ScalingConfiguration(); + } + createScalingConfigurationRequest.setCpu( + scalingConfigurationNew.getCpu() != null + ? scalingConfigurationNew.getCpu() + : scalingConfigurationOld.getCpu()); + if (scalingConfigurationNew.getDataDisks() != null + && scalingConfigurationNew.getDataDisks().size() > 0) { + createScalingConfigurationRequest.setDataDisks(scalingConfigurationNew.getDataDisks()); + } else { + if (scalingConfigurationOld.getDataDisks().size() > 0) { + List dataDisks = new ArrayList<>(); + for (DataDisk dataDisk : scalingConfigurationOld.getDataDisks()) { + CreateScalingConfigurationRequest.DataDisk disk = + new CreateScalingConfigurationRequest.DataDisk(); + disk.setCategory(dataDisk.getCategory()); + disk.setDeleteWithInstance(dataDisk.getDeleteWithInstance()); + disk.setDescription(dataDisk.getDescription()); + disk.setDevice(dataDisk.getDevice()); + disk.setDiskName(dataDisk.getDiskName()); + disk.setEncrypted(dataDisk.getEncrypted()); + disk.setKMSKeyId(dataDisk.getKMSKeyId()); + disk.setSize(dataDisk.getSize()); + disk.setSnapshotId(dataDisk.getSnapshotId()); + dataDisks.add(disk); + } + createScalingConfigurationRequest.setDataDisks(dataDisks); + } + } + createScalingConfigurationRequest.setDeploymentSetId( + StringUtils.isNotEmpty(scalingConfigurationNew.getDeploymentSetId()) + ? scalingConfigurationNew.getDeploymentSetId() + : scalingConfigurationOld.getDeploymentSetId()); + createScalingConfigurationRequest.setHostName( + StringUtils.isNotEmpty(scalingConfigurationNew.getHostName()) + ? scalingConfigurationNew.getHostName() + : scalingConfigurationOld.getHostName()); + createScalingConfigurationRequest.setImageId( + StringUtils.isNotEmpty(scalingConfigurationNew.getImageId()) + ? scalingConfigurationNew.getImageId() + : scalingConfigurationOld.getImageId()); + createScalingConfigurationRequest.setImageName( + StringUtils.isNotEmpty(scalingConfigurationNew.getImageName()) + ? scalingConfigurationNew.getImageName() + : scalingConfigurationOld.getImageName()); + createScalingConfigurationRequest.setInstanceName( + StringUtils.isNotEmpty(scalingConfigurationNew.getInstanceName()) + ? scalingConfigurationNew.getInstanceName() + : scalingConfigurationOld.getInstanceName()); + createScalingConfigurationRequest.setInstanceType( + StringUtils.isNotEmpty(scalingConfigurationNew.getInstanceType()) + ? scalingConfigurationNew.getInstanceType() + : scalingConfigurationOld.getInstanceType()); + if (scalingConfigurationNew.getInstanceTypes() != null + && scalingConfigurationNew.getInstanceTypes().size() > 0) { + createScalingConfigurationRequest.setInstanceTypes( + scalingConfigurationNew.getInstanceTypes()); + } else { + createScalingConfigurationRequest.setInstanceTypes( + scalingConfigurationOld.getInstanceTypes()); + } + createScalingConfigurationRequest.setInternetChargeType( + StringUtils.isNotEmpty(scalingConfigurationNew.getInternetChargeType()) + ? scalingConfigurationNew.getInternetChargeType() + : scalingConfigurationOld.getInternetChargeType()); + createScalingConfigurationRequest.setInternetMaxBandwidthIn( + scalingConfigurationNew.getInternetMaxBandwidthIn() != null + ? scalingConfigurationNew.getInternetMaxBandwidthIn() + : scalingConfigurationOld.getInternetMaxBandwidthIn()); + createScalingConfigurationRequest.setInternetMaxBandwidthOut( + scalingConfigurationNew.getInternetMaxBandwidthOut() != null + ? scalingConfigurationNew.getInternetMaxBandwidthOut() + : scalingConfigurationOld.getInternetMaxBandwidthOut()); + // createScalingConfigurationRequest.setIoOptimized(StringUtils.isNotEmpty(scalingConfigurationNew.getIoOptimized()) ? scalingConfigurationNew.getIoOptimized() : scalingConfigurationOld.getIoOptimized()); + createScalingConfigurationRequest.setKeyPairName( + StringUtils.isNotEmpty(scalingConfigurationNew.getKeyPairName()) + ? scalingConfigurationNew.getKeyPairName() + : scalingConfigurationOld.getKeyPairName()); + createScalingConfigurationRequest.setLoadBalancerWeight( + scalingConfigurationNew.getLoadBalancerWeight() != null + ? scalingConfigurationNew.getLoadBalancerWeight() + : scalingConfigurationOld.getLoadBalancerWeight()); + createScalingConfigurationRequest.setMemory( + scalingConfigurationNew.getMemory() != null + ? scalingConfigurationNew.getMemory() + : scalingConfigurationOld.getMemory()); + createScalingConfigurationRequest.setPassword(scalingConfigurationNew.getPassword()); + createScalingConfigurationRequest.setPasswordInherit( + scalingConfigurationNew.getPasswordInherit() != null + ? scalingConfigurationNew.getPasswordInherit() + : scalingConfigurationOld.getPasswordInherit()); + createScalingConfigurationRequest.setRamRoleName( + StringUtils.isNotEmpty(scalingConfigurationNew.getRamRoleName()) + ? scalingConfigurationNew.getRamRoleName() + : scalingConfigurationOld.getRamRoleName()); + createScalingConfigurationRequest.setResourceGroupId( + StringUtils.isNotEmpty(scalingConfigurationNew.getResourceGroupId()) + ? scalingConfigurationNew.getResourceGroupId() + : scalingConfigurationNew.getResourceGroupId()); + createScalingConfigurationRequest.setScalingConfigurationName( + StringUtils.isNotEmpty(scalingConfigurationNew.getScalingConfigurationName()) + ? scalingConfigurationNew.getScalingConfigurationName() + : scalingConfigurationOld.getScalingConfigurationName()); + createScalingConfigurationRequest.setSecurityEnhancementStrategy( + StringUtils.isNotEmpty(scalingConfigurationNew.getSecurityEnhancementStrategy()) + ? scalingConfigurationNew.getSecurityEnhancementStrategy() + : scalingConfigurationOld.getSecurityEnhancementStrategy()); + createScalingConfigurationRequest.setSecurityGroupId( + StringUtils.isNotEmpty(scalingConfigurationNew.getSecurityGroupId()) + ? scalingConfigurationNew.getSecurityGroupId() + : scalingConfigurationOld.getSecurityGroupId()); + if (scalingConfigurationNew.getSpotPriceLimits() != null + && scalingConfigurationNew.getSpotPriceLimits().size() > 0) { + createScalingConfigurationRequest.setSpotPriceLimits( + scalingConfigurationNew.getSpotPriceLimits()); + } else { + if (scalingConfigurationOld.getSpotPriceLimit().size() > 0) { + List spotPriceLimits = new ArrayList<>(); + for (SpotPriceModel spotPriceModel : scalingConfigurationOld.getSpotPriceLimit()) { + SpotPriceLimit limit = new SpotPriceLimit(); + limit.setInstanceType(spotPriceModel.getInstanceType()); + limit.setPriceLimit(spotPriceModel.getPriceLimit()); + spotPriceLimits.add(limit); + } + createScalingConfigurationRequest.setSpotPriceLimits(spotPriceLimits); + } + } + createScalingConfigurationRequest.setSpotStrategy( + StringUtils.isNotEmpty(scalingConfigurationNew.getSpotStrategy()) + ? scalingConfigurationNew.getSpotStrategy() + : scalingConfigurationOld.getSpotStrategy()); + createScalingConfigurationRequest.setSystemDiskCategory( + StringUtils.isNotEmpty(scalingConfigurationNew.getSystemDiskCategory()) + ? scalingConfigurationNew.getSystemDiskCategory() + : scalingConfigurationOld.getSystemDiskCategory()); + createScalingConfigurationRequest.setSystemDiskDescription( + StringUtils.isNotEmpty(scalingConfigurationNew.getSystemDiskDescription()) + ? scalingConfigurationNew.getSystemDiskDescription() + : scalingConfigurationOld.getSystemDiskDescription()); + createScalingConfigurationRequest.setSystemDiskDiskName( + StringUtils.isNotEmpty(scalingConfigurationNew.getSystemDiskDiskName()) + ? scalingConfigurationNew.getSystemDiskDiskName() + : scalingConfigurationOld.getSystemDiskName()); + createScalingConfigurationRequest.setSystemDiskSize( + scalingConfigurationNew.getSystemDiskSize() != null + ? scalingConfigurationNew.getSystemDiskSize() + : scalingConfigurationOld.getSystemDiskSize()); + if (StringUtils.isNotEmpty(scalingConfigurationNew.getTags())) { + createScalingConfigurationRequest.setTags(scalingConfigurationNew.getTags()); + } else { + List tags = scalingConfigurationOld.getTags(); + if (tags.size() > 0) { + createScalingConfigurationRequest.setTags(gson.toJson(tags)); + } + } + createScalingConfigurationRequest.setUserData( + StringUtils.isNotEmpty(scalingConfigurationNew.getUserData()) + ? scalingConfigurationNew.getUserData() + : scalingConfigurationOld.getUserData()); + return createScalingConfigurationRequest; + } + + private void buildResult(BasicAliCloudDeployDescription description, DeploymentResult result) { + + List serverGroupNames = new ArrayList<>(); + serverGroupNames.add(description.getRegion() + ":" + description.getScalingGroupName()); + result.setServerGroupNames(serverGroupNames); + + Map serverGroupNameByRegion = new HashMap<>(); + serverGroupNameByRegion.put(description.getRegion(), description.getScalingGroupName()); + result.setServerGroupNameByRegion(serverGroupNameByRegion); + + Set deployments = new HashSet<>(); + + DeploymentResult.Deployment.Capacity capacity = new DeploymentResult.Deployment.Capacity(); + capacity.setMax(description.getMaxSize()); + capacity.setMin(description.getMinSize()); + capacity.setDesired(description.getMinSize()); + + DeploymentResult.Deployment deployment = new DeploymentResult.Deployment(); + deployment.setCloudProvider(AliCloudProvider.ID); + deployment.setAccount(description.getCredentials().getName()); + deployment.setCapacity(capacity); + deployment.setLocation(description.getRegion()); + deployment.setServerGroupName(description.getScalingGroupName()); + + deployments.add(deployment); + result.setDeployments(deployments); + } +} diff --git a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/CreateAliCloudServerGroupAtomicOperation.java b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/CreateAliCloudServerGroupAtomicOperation.java new file mode 100644 index 00000000000..0d65a5d210a --- /dev/null +++ b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/CreateAliCloudServerGroupAtomicOperation.java @@ -0,0 +1,426 @@ +/* + * Copyright 2022 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.clouddriver.alicloud.deploy.ops; + +import com.aliyuncs.IAcsClient; +import com.aliyuncs.ess.model.v20140828.*; +import com.aliyuncs.exceptions.ClientException; +import com.aliyuncs.exceptions.ServerException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.netflix.spinnaker.clouddriver.alicloud.AliCloudProvider; +import com.netflix.spinnaker.clouddriver.alicloud.common.ClientFactory; +import com.netflix.spinnaker.clouddriver.alicloud.common.DateParseHelper; +import com.netflix.spinnaker.clouddriver.alicloud.deploy.AliCloudServerGroupNameResolver; +import com.netflix.spinnaker.clouddriver.alicloud.deploy.description.BasicAliCloudDeployDescription; +import com.netflix.spinnaker.clouddriver.alicloud.exception.AliCloudException; +import com.netflix.spinnaker.clouddriver.deploy.DeploymentResult; +import com.netflix.spinnaker.clouddriver.model.ClusterProvider; +import com.netflix.spinnaker.clouddriver.orchestration.AtomicOperation; +import java.util.*; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; +import org.springframework.util.CollectionUtils; + +@Slf4j +public class CreateAliCloudServerGroupAtomicOperation implements AtomicOperation { + + private final List clusterProviders; + + private final ObjectMapper objectMapper; + + private final BasicAliCloudDeployDescription description; + + private final ClientFactory clientFactory; + + public CreateAliCloudServerGroupAtomicOperation( + BasicAliCloudDeployDescription description, + ObjectMapper objectMapper, + ClientFactory clientFactory, + List clusterProviders) { + this.description = description; + this.objectMapper = objectMapper; + this.clientFactory = clientFactory; + this.clusterProviders = clusterProviders; + } + + @Override + public DeploymentResult operate(List priorOutputs) { + DeploymentResult result = new DeploymentResult(); + // create scaling group + IAcsClient client = + clientFactory.createClient( + description.getRegion(), description.getCredentials().getCredentialsProvider()); + + AliCloudServerGroupNameResolver resolver = + new AliCloudServerGroupNameResolver( + description.getCredentials().getName(), description.getRegion(), clusterProviders); + String serverGroupName = + resolver.resolveNextServerGroupName( + description.getApplication(), + description.getStack(), + description.getFreeFormDetails(), + false); + description.setScalingGroupName(serverGroupName); + CreateScalingGroupRequest createScalingGroupRequest = + objectMapper.convertValue(description, CreateScalingGroupRequest.class); + rebuildCreateScalingGroupRequest(description, createScalingGroupRequest); + createScalingGroupRequest.setScalingGroupName(serverGroupName); + if (!StringUtils.isEmpty(description.getVSwitchId())) { + createScalingGroupRequest.setVSwitchId(description.getVSwitchId()); + } + if (description.getVSwitchIds() != null) { + createScalingGroupRequest.setVSwitchIds(description.getVSwitchIds()); + } + CreateScalingGroupResponse createScalingGroupResponse; + try { + createScalingGroupResponse = client.getAcsResponse(createScalingGroupRequest); + description.setScalingGroupId(createScalingGroupResponse.getScalingGroupId()); + } catch (ServerException e) { + log.info(e.getMessage()); + throw new AliCloudException(e.getMessage()); + } catch (ClientException e) { + log.info(e.getMessage()); + throw new AliCloudException(e.getMessage()); + } + + if (StringUtils.isEmpty(description.getScalingGroupId())) { + return result; + } + + String scalingConfigurationId = null; + + // create scaling configuration + for (CreateScalingConfigurationRequest scalingConfiguration : + description.getScalingConfigurations()) { + CreateScalingConfigurationRequest configurationRequest = + objectMapper.convertValue(scalingConfiguration, CreateScalingConfigurationRequest.class); + configurationRequest.setScalingGroupId(description.getScalingGroupId()); + CreateScalingConfigurationResponse configurationResponse; + try { + configurationResponse = client.getAcsResponse(configurationRequest); + scalingConfigurationId = configurationResponse.getScalingConfigurationId(); + } catch (ServerException e) { + log.info(e.getMessage()); + throw new AliCloudException(e.getMessage()); + } catch (ClientException e) { + log.info(e.getMessage()); + throw new AliCloudException(e.getMessage()); + } + } + + if (StringUtils.isEmpty(scalingConfigurationId)) { + return result; + } + + EnableScalingGroupRequest enableScalingGroupRequest = new EnableScalingGroupRequest(); + enableScalingGroupRequest.setScalingGroupId(description.getScalingGroupId()); + enableScalingGroupRequest.setActiveScalingConfigurationId(scalingConfigurationId); + EnableScalingGroupResponse enableScalingGroupResponse; + try { + enableScalingGroupResponse = client.getAcsResponse(enableScalingGroupRequest); + } catch (ServerException e) { + log.info(e.getMessage()); + throw new AliCloudException(e.getMessage()); + } catch (ClientException e) { + log.info(e.getMessage()); + throw new AliCloudException(e.getMessage()); + } + + this.copySourceServerGroupRelatedResource(description); + + buildResult(description, result); + + return result; + } + + private void rebuildCreateScalingGroupRequest( + BasicAliCloudDeployDescription description, CreateScalingGroupRequest request) { + if (description != null + && description.getSource() != null + && description.getSource().getUseSourceCapacity() != null + && description.getSource().getUseSourceCapacity() + && StringUtils.isNotEmpty(description.getSource().getAsgName())) { + + String asgName = description.getSource().getAsgName(); + DescribeScalingGroupsRequest describeScalingGroupsRequest = + new DescribeScalingGroupsRequest(); + describeScalingGroupsRequest.setScalingGroupName(asgName); + DescribeScalingGroupsResponse describeScalingGroupsResponse; + try { + IAcsClient client = + clientFactory.createClient( + description.getRegion(), description.getCredentials().getCredentialsProvider()); + + describeScalingGroupsResponse = client.getAcsResponse(describeScalingGroupsRequest); + if (describeScalingGroupsResponse.getScalingGroups().size() == 0) { + throw new AliCloudException("Old server group is does not exist"); + } + DescribeScalingGroupsResponse.ScalingGroup scalingGroup = + describeScalingGroupsResponse.getScalingGroups().get(0); + if (scalingGroup.getMaxSize() != null) { + request.setMaxSize(scalingGroup.getMaxSize()); + } + if (scalingGroup.getMinSize() != null) { + request.setMinSize(scalingGroup.getMinSize()); + } + } catch (Exception e) { + log.info(e.getMessage()); + throw new AliCloudException(e.getMessage()); + } + } + } + + private void buildResult(BasicAliCloudDeployDescription description, DeploymentResult result) { + + List serverGroupNames = new ArrayList<>(); + serverGroupNames.add(description.getRegion() + ":" + description.getScalingGroupName()); + result.setServerGroupNames(serverGroupNames); + + Map serverGroupNameByRegion = new HashMap<>(); + serverGroupNameByRegion.put(description.getRegion(), description.getScalingGroupName()); + result.setServerGroupNameByRegion(serverGroupNameByRegion); + + Set deployments = new HashSet<>(); + + DeploymentResult.Deployment.Capacity capacity = new DeploymentResult.Deployment.Capacity(); + capacity.setMax(description.getMaxSize()); + capacity.setMin(description.getMinSize()); + capacity.setDesired(description.getMinSize()); + + DeploymentResult.Deployment deployment = new DeploymentResult.Deployment(); + deployment.setCloudProvider(AliCloudProvider.ID); + deployment.setAccount(description.getCredentials().getName()); + deployment.setCapacity(capacity); + deployment.setLocation(description.getRegion()); + deployment.setServerGroupName(description.getScalingGroupName()); + + deployments.add(deployment); + result.setDeployments(deployments); + } + + private void copySourceServerGroupRelatedResource(BasicAliCloudDeployDescription description) { + String sourceScalingGroupId = ""; + String destScalingGroupId = description.getScalingGroupId(); + String region = description.getRegion(); + + String asgName = description.getSource().getAsgName(); + DescribeScalingGroupsRequest describeScalingGroupsRequest = new DescribeScalingGroupsRequest(); + describeScalingGroupsRequest.setScalingGroupName(asgName); + DescribeScalingGroupsResponse describeScalingGroupsResponse; + try { + IAcsClient client = + clientFactory.createClient( + description.getRegion(), description.getCredentials().getCredentialsProvider()); + + describeScalingGroupsResponse = client.getAcsResponse(describeScalingGroupsRequest); + if (describeScalingGroupsResponse.getScalingGroups().size() == 0) { + log.info("Old server group is does not exist"); + return; + } + + DescribeScalingGroupsResponse.ScalingGroup scalingGroup = + describeScalingGroupsResponse.getScalingGroups().get(0); + sourceScalingGroupId = scalingGroup.getScalingGroupId(); + + if (StringUtils.isEmpty(sourceScalingGroupId) || StringUtils.isEmpty(destScalingGroupId)) { + return; + } + + int pageSize = 50; + int pageNumber = 1; + DescribeScalingRulesRequest describeScalingRulesRequest = new DescribeScalingRulesRequest(); + describeScalingRulesRequest.setSysRegionId(region); + describeScalingRulesRequest.setScalingGroupId(sourceScalingGroupId); + describeScalingRulesRequest.setPageSize(pageSize); + describeScalingRulesRequest.setPageNumber(pageNumber); + DescribeScalingRulesResponse describeScalingRulesResponse = + client.getAcsResponse(describeScalingRulesRequest); + Map scalingRuleAriMap = new HashMap<>(); + + while (describeScalingRulesResponse != null + && !CollectionUtils.isEmpty(describeScalingRulesResponse.getScalingRules())) { + for (DescribeScalingRulesResponse.ScalingRule scalingRule : + describeScalingRulesResponse.getScalingRules()) { + // scaling rule + CreateScalingRuleRequest createScalingRuleRequest = + objectMapper.convertValue(scalingRule, CreateScalingRuleRequest.class); + createScalingRuleRequest.setScalingGroupId(destScalingGroupId); + createScalingRuleRequest.setScalingRuleName(""); + CreateScalingRuleResponse createScalingRuleResponse = + client.getAcsResponse(createScalingRuleRequest); + if (createScalingRuleResponse != null + && StringUtils.isNotEmpty(createScalingRuleResponse.getScalingRuleAri())) { + scalingRuleAriMap.put( + scalingRule.getScalingRuleAri(), createScalingRuleResponse.getScalingRuleAri()); + // scheduled task + DescribeScheduledTasksRequest describeScheduledTasksRequest = + new DescribeScheduledTasksRequest(); + describeScheduledTasksRequest.setSysRegionId(region); + describeScheduledTasksRequest.setScheduledAction1(scalingRule.getScalingRuleAri()); + describeScheduledTasksRequest.setPageSize(pageSize); + int taskPageNumber = 1; + describeScheduledTasksRequest.setPageNumber(taskPageNumber); + DescribeScheduledTasksResponse describeScheduledTasksResponse = + client.getAcsResponse(describeScheduledTasksRequest); + + while (describeScheduledTasksResponse != null + && !CollectionUtils.isEmpty(describeScheduledTasksResponse.getScheduledTasks())) { + for (DescribeScheduledTasksResponse.ScheduledTask scheduledTask : + describeScheduledTasksResponse.getScheduledTasks()) { + CreateScheduledTaskRequest createScheduledTaskRequest = + objectMapper.convertValue(scheduledTask, CreateScheduledTaskRequest.class); + + String launchTime = createScheduledTaskRequest.getLaunchTime(); + if (launchTime != null) { + Date launchTimeDate = DateParseHelper.parseUTCTime(launchTime); + Calendar launchTimeCalendar = Calendar.getInstance(); + launchTimeCalendar.setTime(new Date()); + Calendar oldCalendar = Calendar.getInstance(); + oldCalendar.setTime(launchTimeDate); + if (launchTimeDate.before(new Date())) { + launchTimeCalendar.set(Calendar.MINUTE, oldCalendar.get(Calendar.MINUTE)); + launchTimeCalendar.set( + Calendar.HOUR_OF_DAY, oldCalendar.get(Calendar.HOUR_OF_DAY)); + if (launchTimeCalendar.getTime().before(new Date())) { + launchTimeCalendar.add(Calendar.DATE, 1); + } + createScheduledTaskRequest.setLaunchTime( + DateParseHelper.format(launchTimeCalendar.getTime())); + } + } + createScheduledTaskRequest.setSysRegionId(region); + createScheduledTaskRequest.setScheduledAction( + createScalingRuleResponse.getScalingRuleAri()); + createScheduledTaskRequest.setScheduledTaskName(""); + CreateScheduledTaskResponse createScheduledTaskResponse = + client.getAcsResponse(createScheduledTaskRequest); + } + + taskPageNumber = taskPageNumber + 1; + describeScheduledTasksRequest.setPageNumber(taskPageNumber); + describeScheduledTasksResponse = client.getAcsResponse(describeScheduledTasksRequest); + } + } + } + pageNumber = pageNumber + 1; + describeScalingRulesRequest.setPageNumber(pageNumber); + describeScalingRulesResponse = client.getAcsResponse(describeScalingRulesRequest); + } + + // notification configuration + DescribeNotificationConfigurationsRequest describeNotificationConfigurationsRequest = + new DescribeNotificationConfigurationsRequest(); + describeNotificationConfigurationsRequest.setScalingGroupId(sourceScalingGroupId); + DescribeNotificationConfigurationsResponse describeNotificationConfigurationsResponse = + client.getAcsResponse(describeNotificationConfigurationsRequest); + if (describeNotificationConfigurationsResponse != null + && !CollectionUtils.isEmpty( + describeNotificationConfigurationsResponse.getNotificationConfigurationModels())) { + for (DescribeNotificationConfigurationsResponse.NotificationConfigurationModel + notificationConfigurationModel : + describeNotificationConfigurationsResponse.getNotificationConfigurationModels()) { + CreateNotificationConfigurationRequest createNotificationConfigurationRequest = + objectMapper.convertValue( + notificationConfigurationModel, CreateNotificationConfigurationRequest.class); + createNotificationConfigurationRequest.setScalingGroupId(destScalingGroupId); + CreateNotificationConfigurationResponse createNotificationConfigurationResponse = + client.getAcsResponse(createNotificationConfigurationRequest); + } + } + + // lifecycle hooks + DescribeLifecycleHooksRequest describeLifecycleHooksRequest = + new DescribeLifecycleHooksRequest(); + describeLifecycleHooksRequest.setScalingGroupId(sourceScalingGroupId); + describeLifecycleHooksRequest.setPageSize(pageSize); + int hooksPageNumber = 1; + describeLifecycleHooksRequest.setPageNumber(hooksPageNumber); + DescribeLifecycleHooksResponse describeLifecycleHooksResponse = + client.getAcsResponse(describeLifecycleHooksRequest); + + while (describeLifecycleHooksResponse != null + && !CollectionUtils.isEmpty(describeLifecycleHooksResponse.getLifecycleHooks())) { + for (DescribeLifecycleHooksResponse.LifecycleHook lifecycleHook : + describeLifecycleHooksResponse.getLifecycleHooks()) { + CreateLifecycleHookRequest createLifecycleHookRequest = + objectMapper.convertValue(lifecycleHook, CreateLifecycleHookRequest.class); + createLifecycleHookRequest.setScalingGroupId(destScalingGroupId); + CreateLifecycleHookResponse createLifecycleHookResponse = + client.getAcsResponse(createLifecycleHookRequest); + } + hooksPageNumber = hooksPageNumber + 1; + describeLifecycleHooksRequest.setPageNumber(hooksPageNumber); + describeLifecycleHooksResponse = client.getAcsResponse(describeLifecycleHooksRequest); + } + + // alarm + DescribeAlarmsRequest describeAlarmsRequest = new DescribeAlarmsRequest(); + describeAlarmsRequest.setSysRegionId(region); + describeAlarmsRequest.setScalingGroupId(sourceScalingGroupId); + describeAlarmsRequest.setPageSize(pageSize); + int alarmPageNumber = 1; + describeAlarmsRequest.setPageNumber(alarmPageNumber); + DescribeAlarmsResponse describeAlarmsResponse = client.getAcsResponse(describeAlarmsRequest); + + while (describeAlarmsResponse != null + && !CollectionUtils.isEmpty(describeAlarmsResponse.getAlarmList())) { + for (DescribeAlarmsResponse.Alarm alarm : describeAlarmsResponse.getAlarmList()) { + CreateAlarmRequest createAlarmRequest = + objectMapper.convertValue(alarm, CreateAlarmRequest.class); + createAlarmRequest.setSysRegionId(region); + createAlarmRequest.setScalingGroupId(destScalingGroupId); + if (!CollectionUtils.isEmpty(createAlarmRequest.getDimensions())) { + for (CreateAlarmRequest.Dimension dimension : createAlarmRequest.getDimensions()) { + if (sourceScalingGroupId.equals(dimension.getDimensionValue())) { + dimension.setDimensionValue(destScalingGroupId); + } + } + } + createAlarmRequest.setDimensions(createAlarmRequest.getDimensions()); + if (!CollectionUtils.isEmpty(createAlarmRequest.getAlarmActions())) { + List alarmActions = new ArrayList<>(); + for (String alarmAction : createAlarmRequest.getAlarmActions()) { + if (StringUtils.isNotEmpty(scalingRuleAriMap.get(alarmAction))) { + alarmActions.add(scalingRuleAriMap.get(alarmAction)); + } else { + alarmActions.add(alarmAction); + } + } + createAlarmRequest.setAlarmActions(alarmActions); + CreateAlarmResponse createAlarmResponse = client.getAcsResponse(createAlarmRequest); + if (createAlarmResponse != null + && StringUtils.isNotEmpty(createAlarmResponse.getAlarmTaskId()) + && alarm.getEnable() != null + && alarm.getEnable()) { + EnableAlarmRequest enableAlarmRequest = new EnableAlarmRequest(); + enableAlarmRequest.setSysRegionId(region); + enableAlarmRequest.setAlarmTaskId(createAlarmResponse.getAlarmTaskId()); + EnableAlarmResponse enableAlarmResponse = client.getAcsResponse(enableAlarmRequest); + } + } + } + alarmPageNumber = alarmPageNumber + 1; + describeAlarmsRequest.setPageNumber(alarmPageNumber); + describeAlarmsResponse = client.getAcsResponse(describeAlarmsRequest); + } + + } catch (Exception e) { + log.info(e.getMessage()); + return; + } + } +} diff --git a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/DeleteAliCloudLoadBalancerClassicAtomicOperation.java b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/DeleteAliCloudLoadBalancerClassicAtomicOperation.java index 5171db4cc92..eec36111dff 100644 --- a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/DeleteAliCloudLoadBalancerClassicAtomicOperation.java +++ b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/DeleteAliCloudLoadBalancerClassicAtomicOperation.java @@ -26,17 +26,12 @@ import com.netflix.spinnaker.clouddriver.alicloud.deploy.description.UpsertAliCloudLoadBalancerDescription; import com.netflix.spinnaker.clouddriver.alicloud.exception.AliCloudException; import com.netflix.spinnaker.clouddriver.orchestration.AtomicOperation; -import groovy.util.logging.Slf4j; import java.util.List; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import lombok.extern.slf4j.Slf4j; @Slf4j public class DeleteAliCloudLoadBalancerClassicAtomicOperation implements AtomicOperation { - private final Logger log = - LoggerFactory.getLogger(DeleteAliCloudLoadBalancerClassicAtomicOperation.class); - private final UpsertAliCloudLoadBalancerDescription description; private final ClientFactory clientFactory; @@ -52,9 +47,7 @@ public Void operate(List priorOutputs) { IAcsClient client = clientFactory.createClient( - description.getRegion(), - description.getCredentials().getAccessKeyId(), - description.getCredentials().getAccessSecretKey()); + description.getRegion(), description.getCredentials().getCredentialsProvider()); DescribeLoadBalancersResponse.LoadBalancer loadBalancerT = null; DescribeLoadBalancersRequest queryRequest = new DescribeLoadBalancersRequest(); queryRequest.setLoadBalancerName(description.getLoadBalancerName()); diff --git a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/DeleteAliCloudSecurityGroupAtomicOperation.java b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/DeleteAliCloudSecurityGroupAtomicOperation.java index 3341324876b..d0bd4082524 100644 --- a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/DeleteAliCloudSecurityGroupAtomicOperation.java +++ b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/DeleteAliCloudSecurityGroupAtomicOperation.java @@ -1,5 +1,5 @@ /* - * Copyright 2019 Alibaba Group. + * Copyright 2022 Netflix, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -27,17 +27,12 @@ import com.netflix.spinnaker.clouddriver.alicloud.deploy.description.DeleteAliCloudSecurityGroupDescription; import com.netflix.spinnaker.clouddriver.alicloud.exception.AliCloudException; import com.netflix.spinnaker.clouddriver.orchestration.AtomicOperation; -import groovy.util.logging.Slf4j; import java.util.List; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import lombok.extern.slf4j.Slf4j; @Slf4j public class DeleteAliCloudSecurityGroupAtomicOperation implements AtomicOperation { - private final Logger log = - LoggerFactory.getLogger(DeleteAliCloudSecurityGroupAtomicOperation.class); - private final DeleteAliCloudSecurityGroupDescription description; private final ClientFactory clientFactory; @@ -52,10 +47,8 @@ public DeleteAliCloudSecurityGroupAtomicOperation( public Void operate(List priorOutputs) { for (String region : description.getRegions()) { IAcsClient client = - clientFactory.createClient( - region, - description.getCredentials().getAccessKeyId(), - description.getCredentials().getAccessSecretKey()); + clientFactory.createClient(region, description.getCredentials().getCredentialsProvider()); + DescribeSecurityGroupsRequest describeSecurityGroupsRequest = new DescribeSecurityGroupsRequest(); describeSecurityGroupsRequest.setSecurityGroupName(description.getSecurityGroupName()); @@ -72,10 +65,10 @@ public Void operate(List priorOutputs) { } catch (ServerException e) { log.info(e.getMessage()); - throw new AliCloudException(e); + throw new AliCloudException(e.getMessage()); } catch (ClientException e) { log.info(e.getMessage()); - throw new AliCloudException(e); + throw new AliCloudException(e.getMessage()); } } return null; diff --git a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/DestroyAliCloudServerGroupAtomicOperation.java b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/DestroyAliCloudServerGroupAtomicOperation.java new file mode 100644 index 00000000000..6867f5f137a --- /dev/null +++ b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/DestroyAliCloudServerGroupAtomicOperation.java @@ -0,0 +1,83 @@ +/* + * Copyright 2022 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.clouddriver.alicloud.deploy.ops; + +import com.aliyuncs.IAcsClient; +import com.aliyuncs.ess.model.v20140828.DeleteScalingGroupRequest; +import com.aliyuncs.ess.model.v20140828.DescribeScalingGroupsRequest; +import com.aliyuncs.ess.model.v20140828.DescribeScalingGroupsResponse; +import com.aliyuncs.ess.model.v20140828.DescribeScalingGroupsResponse.ScalingGroup; +import com.aliyuncs.exceptions.ClientException; +import com.aliyuncs.exceptions.ServerException; +import com.netflix.spinnaker.clouddriver.alicloud.common.ClientFactory; +import com.netflix.spinnaker.clouddriver.alicloud.deploy.description.DestroyAliCloudServerGroupDescription; +import com.netflix.spinnaker.clouddriver.alicloud.exception.AliCloudException; +import com.netflix.spinnaker.clouddriver.orchestration.AtomicOperation; +import java.util.List; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; + +@Slf4j +public class DestroyAliCloudServerGroupAtomicOperation implements AtomicOperation { + + private final DestroyAliCloudServerGroupDescription description; + + private final ClientFactory clientFactory; + + public DestroyAliCloudServerGroupAtomicOperation( + DestroyAliCloudServerGroupDescription description, ClientFactory clientFactory) { + this.description = description; + this.clientFactory = clientFactory; + } + + @Override + public Void operate(List priorOutputs) { + IAcsClient client = + clientFactory.createClient( + description.getRegion(), description.getCredentials().getCredentialsProvider()); + + DescribeScalingGroupsRequest describeScalingGroupsRequest = new DescribeScalingGroupsRequest(); + if (StringUtils.isNotEmpty(description.getServerGroupName())) { + describeScalingGroupsRequest.setScalingGroupName(description.getServerGroupName()); + } else if (StringUtils.isNotEmpty(description.getScalingGroupName())) { + describeScalingGroupsRequest.setScalingGroupName(description.getScalingGroupName()); + } else { + throw new AliCloudException("Not passed Server Group"); + } + + describeScalingGroupsRequest.setPageSize(50); + DescribeScalingGroupsResponse describeScalingGroupsResponse; + try { + describeScalingGroupsResponse = client.getAcsResponse(describeScalingGroupsRequest); + for (ScalingGroup scalingGroup : describeScalingGroupsResponse.getScalingGroups()) { + DeleteScalingGroupRequest deleteScalingGroupRequest = new DeleteScalingGroupRequest(); + deleteScalingGroupRequest.setScalingGroupId(scalingGroup.getScalingGroupId()); + deleteScalingGroupRequest.setForceDelete(true); + client.getAcsResponse(deleteScalingGroupRequest); + } + + } catch (ServerException e) { + log.info(e.getMessage()); + throw new AliCloudException(e.getMessage()); + } catch (ClientException e) { + log.info(e.getMessage()); + throw new AliCloudException(e.getMessage()); + } + + return null; + } +} diff --git a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/DisableAliCloudServerGroupAtomicOperation.java b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/DisableAliCloudServerGroupAtomicOperation.java new file mode 100644 index 00000000000..ee4bfa9c20d --- /dev/null +++ b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/DisableAliCloudServerGroupAtomicOperation.java @@ -0,0 +1,109 @@ +/* + * Copyright 2022 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.clouddriver.alicloud.deploy.ops; + +import com.aliyuncs.IAcsClient; +import com.aliyuncs.ess.model.v20140828.DescribeScalingGroupsRequest; +import com.aliyuncs.ess.model.v20140828.DescribeScalingGroupsResponse; +import com.aliyuncs.ess.model.v20140828.DescribeScalingGroupsResponse.ScalingGroup; +import com.aliyuncs.ess.model.v20140828.DescribeScalingInstancesRequest; +import com.aliyuncs.ess.model.v20140828.DescribeScalingInstancesResponse; +import com.aliyuncs.ess.model.v20140828.DescribeScalingInstancesResponse.ScalingInstance; +import com.aliyuncs.ess.model.v20140828.DisableScalingGroupRequest; +import com.aliyuncs.ess.model.v20140828.RemoveInstancesRequest; +import com.aliyuncs.exceptions.ClientException; +import com.aliyuncs.exceptions.ServerException; +import com.netflix.spinnaker.clouddriver.alicloud.common.ClientFactory; +import com.netflix.spinnaker.clouddriver.alicloud.deploy.description.DisableAliCloudServerGroupDescription; +import com.netflix.spinnaker.clouddriver.alicloud.exception.AliCloudException; +import com.netflix.spinnaker.clouddriver.orchestration.AtomicOperation; +import java.util.ArrayList; +import java.util.List; +import lombok.extern.slf4j.Slf4j; + +@Slf4j +public class DisableAliCloudServerGroupAtomicOperation implements AtomicOperation { + + private final DisableAliCloudServerGroupDescription description; + + private final ClientFactory clientFactory; + + public DisableAliCloudServerGroupAtomicOperation( + DisableAliCloudServerGroupDescription description, ClientFactory clientFactory) { + this.description = description; + this.clientFactory = clientFactory; + } + + @Override + public Void operate(List priorOutputs) { + + IAcsClient client = + clientFactory.createClient( + description.getRegion(), description.getCredentials().getCredentialsProvider()); + + DescribeScalingGroupsRequest describeScalingGroupsRequest = new DescribeScalingGroupsRequest(); + describeScalingGroupsRequest.setScalingGroupName(description.getServerGroupName()); + describeScalingGroupsRequest.setPageSize(50); + DescribeScalingGroupsResponse describeScalingGroupsResponse; + try { + describeScalingGroupsResponse = client.getAcsResponse(describeScalingGroupsRequest); + for (ScalingGroup scalingGroup : describeScalingGroupsResponse.getScalingGroups()) { + if ("Active".equals(scalingGroup.getLifecycleState())) { + Integer maxSize = scalingGroup.getMaxSize(); + Integer minSize = scalingGroup.getMinSize(); + if (maxSize != null && maxSize == 0 && minSize != null && minSize == 0) { + // Number of query instances + DescribeScalingInstancesRequest scalingInstancesRequest = + new DescribeScalingInstancesRequest(); + scalingInstancesRequest.setScalingGroupId(scalingGroup.getScalingGroupId()); + scalingInstancesRequest.setScalingConfigurationId( + scalingGroup.getActiveScalingConfigurationId()); + scalingInstancesRequest.setPageSize(50); + DescribeScalingInstancesResponse scalingInstancesResponse = + client.getAcsResponse(scalingInstancesRequest); + List scalingInstances = scalingInstancesResponse.getScalingInstances(); + if (scalingInstances != null && scalingInstances.size() > 0) { + // Remove instance + List instanceIds = new ArrayList<>(); + scalingInstances.forEach( + scalingInstance -> { + instanceIds.add(scalingInstance.getInstanceId()); + }); + RemoveInstancesRequest removeInstancesRequest = new RemoveInstancesRequest(); + removeInstancesRequest.setInstanceIds(instanceIds); + removeInstancesRequest.setScalingGroupId(scalingGroup.getScalingGroupId()); + client.getAcsResponse(removeInstancesRequest); + } + } + + DisableScalingGroupRequest disableScalingGroupRequest = new DisableScalingGroupRequest(); + disableScalingGroupRequest.setScalingGroupId(scalingGroup.getScalingGroupId()); + client.getAcsResponse(disableScalingGroupRequest); + } + } + + } catch (ServerException e) { + log.info(e.getMessage()); + throw new AliCloudException(e.getMessage()); + } catch (ClientException e) { + log.info(e.getMessage()); + throw new AliCloudException(e.getMessage()); + } + + return null; + } +} diff --git a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/EnableAliCloudServerGroupAtomicOperation.java b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/EnableAliCloudServerGroupAtomicOperation.java new file mode 100644 index 00000000000..3612a2dd9c7 --- /dev/null +++ b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/EnableAliCloudServerGroupAtomicOperation.java @@ -0,0 +1,90 @@ +/* + * Copyright 2022 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.clouddriver.alicloud.deploy.ops; + +import com.aliyuncs.IAcsClient; +import com.aliyuncs.ess.model.v20140828.DescribeScalingConfigurationsRequest; +import com.aliyuncs.ess.model.v20140828.DescribeScalingConfigurationsResponse; +import com.aliyuncs.ess.model.v20140828.DescribeScalingConfigurationsResponse.ScalingConfiguration; +import com.aliyuncs.ess.model.v20140828.DescribeScalingGroupsRequest; +import com.aliyuncs.ess.model.v20140828.DescribeScalingGroupsResponse; +import com.aliyuncs.ess.model.v20140828.DescribeScalingGroupsResponse.ScalingGroup; +import com.aliyuncs.ess.model.v20140828.EnableScalingGroupRequest; +import com.aliyuncs.exceptions.ClientException; +import com.aliyuncs.exceptions.ServerException; +import com.netflix.spinnaker.clouddriver.alicloud.common.ClientFactory; +import com.netflix.spinnaker.clouddriver.alicloud.deploy.description.EnableAliCloudServerGroupDescription; +import com.netflix.spinnaker.clouddriver.alicloud.exception.AliCloudException; +import com.netflix.spinnaker.clouddriver.orchestration.AtomicOperation; +import java.util.List; +import lombok.extern.slf4j.Slf4j; + +@Slf4j +public class EnableAliCloudServerGroupAtomicOperation implements AtomicOperation { + + private final EnableAliCloudServerGroupDescription description; + + private final ClientFactory clientFactory; + + public EnableAliCloudServerGroupAtomicOperation( + EnableAliCloudServerGroupDescription description, ClientFactory clientFactory) { + this.description = description; + this.clientFactory = clientFactory; + } + + @Override + public Void operate(List priorOutputs) { + IAcsClient client = + clientFactory.createClient( + description.getRegion(), description.getCredentials().getCredentialsProvider()); + + DescribeScalingGroupsRequest describeScalingGroupsRequest = new DescribeScalingGroupsRequest(); + describeScalingGroupsRequest.setScalingGroupName(description.getServerGroupName()); + describeScalingGroupsRequest.setPageSize(50); + DescribeScalingGroupsResponse describeScalingGroupsResponse; + try { + describeScalingGroupsResponse = client.getAcsResponse(describeScalingGroupsRequest); + for (ScalingGroup scalingGroup : describeScalingGroupsResponse.getScalingGroups()) { + if ("Inactive".equals(scalingGroup.getLifecycleState())) { + DescribeScalingConfigurationsRequest configurationsRequest = + new DescribeScalingConfigurationsRequest(); + configurationsRequest.setScalingGroupId(scalingGroup.getScalingGroupId()); + DescribeScalingConfigurationsResponse configurationsResponse = + client.getAcsResponse(configurationsRequest); + if (configurationsResponse.getScalingConfigurations().size() > 0) { + ScalingConfiguration scalingConfiguration = + configurationsResponse.getScalingConfigurations().get(0); + EnableScalingGroupRequest enableScalingGroupRequest = new EnableScalingGroupRequest(); + enableScalingGroupRequest.setScalingGroupId(scalingGroup.getScalingGroupId()); + enableScalingGroupRequest.setActiveScalingConfigurationId( + scalingConfiguration.getScalingConfigurationId()); + client.getAcsResponse(enableScalingGroupRequest); + } + } + } + + } catch (ServerException e) { + log.info(e.getMessage()); + throw new AliCloudException(e.getMessage()); + } catch (ClientException e) { + log.info(e.getMessage()); + throw new AliCloudException(e.getMessage()); + } + + return null; + } +} diff --git a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/ModifyScalingGroupAtomicOperation.java b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/ModifyScalingGroupAtomicOperation.java new file mode 100644 index 00000000000..75462833bd1 --- /dev/null +++ b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/ModifyScalingGroupAtomicOperation.java @@ -0,0 +1,87 @@ +/* + * Copyright 2022 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.clouddriver.alicloud.deploy.ops; + +import com.aliyuncs.IAcsClient; +import com.aliyuncs.ess.model.v20140828.DescribeScalingGroupsRequest; +import com.aliyuncs.ess.model.v20140828.DescribeScalingGroupsResponse; +import com.aliyuncs.ess.model.v20140828.DescribeScalingGroupsResponse.ScalingGroup; +import com.aliyuncs.ess.model.v20140828.ModifyScalingGroupRequest; +import com.aliyuncs.exceptions.ClientException; +import com.aliyuncs.exceptions.ServerException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.netflix.spinnaker.clouddriver.alicloud.common.ClientFactory; +import com.netflix.spinnaker.clouddriver.alicloud.deploy.description.ModifyScalingGroupDescription; +import com.netflix.spinnaker.clouddriver.alicloud.exception.AliCloudException; +import com.netflix.spinnaker.clouddriver.orchestration.AtomicOperation; +import java.util.List; +import lombok.extern.slf4j.Slf4j; + +@Slf4j +public class ModifyScalingGroupAtomicOperation implements AtomicOperation { + + private final ModifyScalingGroupDescription description; + + private final ObjectMapper objectMapper; + + private final ClientFactory clientFactory; + + public ModifyScalingGroupAtomicOperation( + ModifyScalingGroupDescription description, + ObjectMapper objectMapper, + ClientFactory clientFactory) { + this.description = description; + this.objectMapper = objectMapper; + this.clientFactory = clientFactory; + } + + @Override + public Void operate(List priorOutputs) { + + for (ModifyScalingGroupDescription.ScalingGroup scalingGroup : description.getScalingGroups()) { + IAcsClient client = + clientFactory.createClient( + description.getRegion(), description.getCredentials().getCredentialsProvider()); + DescribeScalingGroupsRequest describeScalingGroupsRequest = + new DescribeScalingGroupsRequest(); + describeScalingGroupsRequest.setScalingGroupName(scalingGroup.getScalingGroupName()); + describeScalingGroupsRequest.setPageSize(50); + DescribeScalingGroupsResponse describeScalingGroupsResponse; + try { + describeScalingGroupsResponse = client.getAcsResponse(describeScalingGroupsRequest); + for (ScalingGroup scaling : describeScalingGroupsResponse.getScalingGroups()) { + ModifyScalingGroupRequest modifyScalingGroupRequest = + objectMapper.convertValue(description, ModifyScalingGroupRequest.class); + modifyScalingGroupRequest.setScalingGroupId(scaling.getScalingGroupId()); + if (description.getVSwitchIds() != null) { + modifyScalingGroupRequest.setVSwitchIds(description.getVSwitchIds()); + } + client.getAcsResponse(modifyScalingGroupRequest); + } + + } catch (ServerException e) { + log.info(e.getMessage()); + throw new AliCloudException(e.getMessage()); + } catch (ClientException e) { + log.info(e.getMessage()); + throw new AliCloudException(e.getMessage()); + } + } + + return null; + } +} diff --git a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/ResizeAliCloudServerGroupAtomicOperation.java b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/ResizeAliCloudServerGroupAtomicOperation.java new file mode 100644 index 00000000000..1e3b5fde578 --- /dev/null +++ b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/ResizeAliCloudServerGroupAtomicOperation.java @@ -0,0 +1,77 @@ +/* + * Copyright 2022 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.clouddriver.alicloud.deploy.ops; + +import com.aliyuncs.IAcsClient; +import com.aliyuncs.ess.model.v20140828.DescribeScalingGroupsRequest; +import com.aliyuncs.ess.model.v20140828.DescribeScalingGroupsResponse; +import com.aliyuncs.ess.model.v20140828.DescribeScalingGroupsResponse.ScalingGroup; +import com.aliyuncs.ess.model.v20140828.ModifyScalingGroupRequest; +import com.aliyuncs.exceptions.ClientException; +import com.aliyuncs.exceptions.ServerException; +import com.netflix.spinnaker.clouddriver.alicloud.common.ClientFactory; +import com.netflix.spinnaker.clouddriver.alicloud.deploy.description.ResizeAliCloudServerGroupDescription; +import com.netflix.spinnaker.clouddriver.alicloud.exception.AliCloudException; +import com.netflix.spinnaker.clouddriver.orchestration.AtomicOperation; +import java.util.LinkedHashMap; +import java.util.List; +import lombok.extern.slf4j.Slf4j; + +@Slf4j +public class ResizeAliCloudServerGroupAtomicOperation implements AtomicOperation { + + private final ResizeAliCloudServerGroupDescription description; + + private final ClientFactory clientFactory; + + public ResizeAliCloudServerGroupAtomicOperation( + ResizeAliCloudServerGroupDescription description, ClientFactory clientFactory) { + this.description = description; + this.clientFactory = clientFactory; + } + + @Override + public Void operate(List priorOutputs) { + IAcsClient client = + clientFactory.createClient( + description.getRegion(), description.getCredentials().getCredentialsProvider()); + DescribeScalingGroupsRequest describeScalingGroupsRequest = new DescribeScalingGroupsRequest(); + describeScalingGroupsRequest.setScalingGroupName(description.getServerGroupName()); + describeScalingGroupsRequest.setPageSize(50); + DescribeScalingGroupsResponse describeScalingGroupsResponse; + try { + describeScalingGroupsResponse = client.getAcsResponse(describeScalingGroupsRequest); + for (ScalingGroup scalingGroup : describeScalingGroupsResponse.getScalingGroups()) { + ModifyScalingGroupRequest modifyScalingGroupRequest = new ModifyScalingGroupRequest(); + modifyScalingGroupRequest.setScalingGroupId(scalingGroup.getScalingGroupId()); + LinkedHashMap capacity = description.getCapacity(); + modifyScalingGroupRequest.setMaxSize(capacity.get("max")); + modifyScalingGroupRequest.setMinSize(capacity.get("min")); + client.getAcsResponse(modifyScalingGroupRequest); + } + + } catch (ServerException e) { + log.info(e.getMessage()); + throw new AliCloudException(e.getMessage()); + } catch (ClientException e) { + log.info(e.getMessage()); + throw new AliCloudException(e.getMessage()); + } + + return null; + } +} diff --git a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/UpdateAliCloudLaunchConfigAtomicOperation.java b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/UpdateAliCloudLaunchConfigAtomicOperation.java new file mode 100644 index 00000000000..f734a66f99f --- /dev/null +++ b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/UpdateAliCloudLaunchConfigAtomicOperation.java @@ -0,0 +1,85 @@ +/* + * Copyright 2022 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.clouddriver.alicloud.deploy.ops; + +import com.aliyuncs.IAcsClient; +import com.aliyuncs.ess.model.v20140828.DescribeScalingGroupsRequest; +import com.aliyuncs.ess.model.v20140828.DescribeScalingGroupsResponse; +import com.aliyuncs.ess.model.v20140828.DescribeScalingGroupsResponse.ScalingGroup; +import com.aliyuncs.ess.model.v20140828.ModifyScalingConfigurationRequest; +import com.aliyuncs.exceptions.ClientException; +import com.aliyuncs.exceptions.ServerException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.netflix.spinnaker.clouddriver.alicloud.common.ClientFactory; +import com.netflix.spinnaker.clouddriver.alicloud.deploy.description.UpdateAliCloudLaunchConfigDescription; +import com.netflix.spinnaker.clouddriver.alicloud.exception.AliCloudException; +import com.netflix.spinnaker.clouddriver.orchestration.AtomicOperation; +import java.util.List; +import lombok.extern.slf4j.Slf4j; + +@Slf4j +public class UpdateAliCloudLaunchConfigAtomicOperation implements AtomicOperation { + + private final UpdateAliCloudLaunchConfigDescription description; + + private final ObjectMapper objectMapper; + + private final ClientFactory clientFactory; + + public UpdateAliCloudLaunchConfigAtomicOperation( + UpdateAliCloudLaunchConfigDescription description, + ObjectMapper objectMapper, + ClientFactory clientFactory) { + this.description = description; + this.objectMapper = objectMapper; + this.clientFactory = clientFactory; + } + + @Override + public Void operate(List priorOutputs) { + IAcsClient client = + clientFactory.createClient( + description.getRegion(), description.getCredentials().getCredentialsProvider()); + DescribeScalingGroupsRequest describeScalingGroupsRequest = new DescribeScalingGroupsRequest(); + describeScalingGroupsRequest.setScalingGroupName(description.getServerGroupName()); + describeScalingGroupsRequest.setPageSize(50); + DescribeScalingGroupsResponse describeScalingGroupsResponse; + try { + describeScalingGroupsResponse = client.getAcsResponse(describeScalingGroupsRequest); + for (ScalingGroup scalingGroup : describeScalingGroupsResponse.getScalingGroups()) { + String activeScalingConfigurationId = scalingGroup.getActiveScalingConfigurationId(); + ModifyScalingConfigurationRequest configurationRequest = + objectMapper.convertValue(description, ModifyScalingConfigurationRequest.class); + configurationRequest.setScalingConfigurationId(activeScalingConfigurationId); + if (description.getSecurityGroups() != null) { + for (String securityGroupId : description.getSecurityGroups()) { + configurationRequest.setSecurityGroupId(securityGroupId); + } + } + client.getAcsResponse(configurationRequest); + } + } catch (ServerException e) { + log.info(e.getMessage()); + throw new AliCloudException(e.getMessage()); + } catch (ClientException e) { + log.info(e.getMessage()); + throw new AliCloudException(e.getMessage()); + } + + return null; + } +} diff --git a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/UpsertAliCloudImageTagsAtomicOperation.java b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/UpsertAliCloudImageTagsAtomicOperation.java new file mode 100644 index 00000000000..603b2d19614 --- /dev/null +++ b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/UpsertAliCloudImageTagsAtomicOperation.java @@ -0,0 +1,91 @@ +/* + * Copyright 2022 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.clouddriver.alicloud.deploy.ops; + +import com.aliyuncs.IAcsClient; +import com.aliyuncs.ecs.model.v20140526.DescribeImagesRequest; +import com.aliyuncs.ecs.model.v20140526.DescribeImagesResponse; +import com.aliyuncs.ecs.model.v20140526.TagResourcesRequest; +import com.aliyuncs.exceptions.ClientException; +import com.aliyuncs.exceptions.ServerException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.netflix.spinnaker.clouddriver.alicloud.common.ClientFactory; +import com.netflix.spinnaker.clouddriver.alicloud.deploy.description.UpsertAliCloudImageTagsDescription; +import com.netflix.spinnaker.clouddriver.alicloud.exception.AliCloudException; +import com.netflix.spinnaker.clouddriver.orchestration.AtomicOperation; +import java.util.ArrayList; +import java.util.List; + +public class UpsertAliCloudImageTagsAtomicOperation implements AtomicOperation { + + private final ObjectMapper objectMapper; + + private final UpsertAliCloudImageTagsDescription description; + + private final ClientFactory clientFactory; + + public UpsertAliCloudImageTagsAtomicOperation( + UpsertAliCloudImageTagsDescription description, + ObjectMapper objectMapper, + ClientFactory clientFactory) { + this.description = description; + this.objectMapper = objectMapper; + this.clientFactory = clientFactory; + } + + @Override + public Void operate(List priorOutputs) { + IAcsClient client = + clientFactory.createClient( + description.getRegion(), description.getCredentials().getCredentialsProvider()); + DescribeImagesRequest describeImagesRequest = new DescribeImagesRequest(); + describeImagesRequest.setSysRegionId(description.getRegion()); + describeImagesRequest.setImageName(description.getImageName()); + try { + DescribeImagesResponse acsResponse = client.getAcsResponse(describeImagesRequest); + if (acsResponse.getImages() != null && acsResponse.getImages().size() > 0) { + TagResourcesRequest tagResourcesRequest = new TagResourcesRequest(); + List imageIds = new ArrayList<>(1); + imageIds.add(acsResponse.getImages().get(0).getImageId()); + tagResourcesRequest.setSysRegionId(description.getRegion()); + tagResourcesRequest.setResourceIds(imageIds); + tagResourcesRequest.setResourceType("image"); + List tags = new ArrayList<>(description.getTags().size()); + description + .getTags() + .forEach( + (k, v) -> { + TagResourcesRequest.Tag tag = new TagResourcesRequest.Tag(); + tag.setKey(k); + tag.setValue(v); + tags.add(tag); + }); + tagResourcesRequest.setTags(tags); + client.getAcsResponse(tagResourcesRequest); + } else { + throw new AliCloudException("The image not fond"); + } + } catch (ServerException e) { + e.printStackTrace(); + throw new AliCloudException(e.getMessage()); + } catch (ClientException e) { + e.printStackTrace(); + throw new AliCloudException(e.getMessage()); + } + return null; + } +} diff --git a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/UpsertAliCloudLoadBalancerAtomicOperation.java b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/UpsertAliCloudLoadBalancerAtomicOperation.java index 92676627949..60eab04fe79 100644 --- a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/UpsertAliCloudLoadBalancerAtomicOperation.java +++ b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/UpsertAliCloudLoadBalancerAtomicOperation.java @@ -44,23 +44,18 @@ import com.netflix.spinnaker.clouddriver.data.task.Task; import com.netflix.spinnaker.clouddriver.data.task.TaskRepository; import com.netflix.spinnaker.clouddriver.orchestration.AtomicOperation; -import groovy.util.logging.Slf4j; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; +import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; @Slf4j public class UpsertAliCloudLoadBalancerAtomicOperation implements AtomicOperation { - private final Logger logger = - LoggerFactory.getLogger(UpsertAliCloudLoadBalancerAtomicOperation.class); - private final ObjectMapper objectMapper; private final UpsertAliCloudLoadBalancerDescription description; @@ -94,9 +89,7 @@ public Map operate(List priorOutputs) { IAcsClient client = clientFactory.createClient( - description.getRegion(), - description.getCredentials().getAccessKeyId(), - description.getCredentials().getAccessSecretKey()); + description.getRegion(), description.getCredentials().getCredentialsProvider()); DescribeLoadBalancersResponse.LoadBalancer loadBalancerT = null; // Create or Update load balancing instances // Query all load balancing instances under this user @@ -112,10 +105,10 @@ public Map operate(List priorOutputs) { } } catch (ServerException e) { - logger.info(e.getMessage()); + log.error(e.getMessage()); throw new AliCloudException(e.getMessage()); } catch (ClientException e) { - logger.info(e.getMessage()); + log.error(e.getMessage()); throw new AliCloudException(e.getMessage()); } @@ -140,10 +133,10 @@ public Map operate(List priorOutputs) { description.setLoadBalancerId(loadBalancerResponse.getLoadBalancerId()); } catch (ServerException e) { - logger.info(e.getMessage()); + log.error(e.getMessage()); throw new AliCloudException(e.getMessage()); } catch (ClientException e) { - logger.info(e.getMessage()); + log.error(e.getMessage()); throw new AliCloudException(e.getMessage()); } } @@ -153,12 +146,12 @@ public Map operate(List priorOutputs) { } try { - createListener(loadBalancerT == null ? false : true, client); + createListener(loadBalancerT != null, client); } catch (ServerException e) { - logger.info(e.getMessage()); + log.error(e.getMessage()); throw new AliCloudException(e.getMessage()); } catch (ClientException e) { - logger.info(e.getMessage()); + log.error(e.getMessage()); throw new AliCloudException(e.getMessage()); } @@ -169,10 +162,10 @@ public Map operate(List priorOutputs) { try { client.getAcsResponse(statusRequest); } catch (ServerException e) { - logger.info(e.getMessage()); + log.error(e.getMessage()); throw new AliCloudException(e.getMessage()); } catch (ClientException e) { - logger.info(e.getMessage()); + log.error(e.getMessage()); throw new AliCloudException(e.getMessage()); } @@ -293,6 +286,10 @@ private void createListener(boolean whetherToCreate, IAcsClient client) throws C private void addListener(List createListenerList, IAcsClient client) throws ClientException { for (Listener listener : createListenerList) { + if (listener.getListenerProtocal() == null) { + createHTTPListener(client, listener); + continue; + } switch (listener.getListenerProtocal()) { case HTTPS: createHTTPSListener(client, listener); diff --git a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/UpsertAliCloudSecurityGroupAtomicOperation.java b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/UpsertAliCloudSecurityGroupAtomicOperation.java index 77202ac83f4..3465fe71b00 100644 --- a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/UpsertAliCloudSecurityGroupAtomicOperation.java +++ b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/UpsertAliCloudSecurityGroupAtomicOperation.java @@ -1,5 +1,5 @@ /* - * Copyright 2019 Alibaba Group. + * Copyright 2022 Netflix, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,9 +17,17 @@ package com.netflix.spinnaker.clouddriver.alicloud.deploy.ops; import com.aliyuncs.IAcsClient; -import com.aliyuncs.ecs.model.v20140526.*; +import com.aliyuncs.ecs.model.v20140526.AuthorizeSecurityGroupRequest; +import com.aliyuncs.ecs.model.v20140526.CreateSecurityGroupRequest; +import com.aliyuncs.ecs.model.v20140526.CreateSecurityGroupResponse; +import com.aliyuncs.ecs.model.v20140526.DescribeSecurityGroupAttributeRequest; +import com.aliyuncs.ecs.model.v20140526.DescribeSecurityGroupAttributeResponse; import com.aliyuncs.ecs.model.v20140526.DescribeSecurityGroupAttributeResponse.Permission; +import com.aliyuncs.ecs.model.v20140526.DescribeSecurityGroupsRequest; +import com.aliyuncs.ecs.model.v20140526.DescribeSecurityGroupsResponse; import com.aliyuncs.ecs.model.v20140526.DescribeSecurityGroupsResponse.SecurityGroup; +import com.aliyuncs.ecs.model.v20140526.ModifySecurityGroupRuleRequest; +import com.aliyuncs.ecs.model.v20140526.RevokeSecurityGroupRequest; import com.aliyuncs.exceptions.ClientException; import com.aliyuncs.exceptions.ServerException; import com.fasterxml.jackson.databind.ObjectMapper; @@ -27,21 +35,16 @@ import com.netflix.spinnaker.clouddriver.alicloud.deploy.description.UpsertAliCloudSecurityGroupDescription; import com.netflix.spinnaker.clouddriver.alicloud.exception.AliCloudException; import com.netflix.spinnaker.clouddriver.orchestration.AtomicOperation; -import groovy.util.logging.Slf4j; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Objects; +import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; @Slf4j public class UpsertAliCloudSecurityGroupAtomicOperation implements AtomicOperation { - private final Logger log = - LoggerFactory.getLogger(UpsertAliCloudSecurityGroupAtomicOperation.class); - private final UpsertAliCloudSecurityGroupDescription description; private final ClientFactory clientFactory; @@ -61,9 +64,7 @@ public UpsertAliCloudSecurityGroupAtomicOperation( public Void operate(List priorOutputs) { IAcsClient client = clientFactory.createClient( - description.getRegion(), - description.getCredentials().getAccessKeyId(), - description.getCredentials().getAccessSecretKey()); + description.getRegion(), description.getCredentials().getCredentialsProvider()); DescribeSecurityGroupsRequest describeSecurityGroupsRequest = new DescribeSecurityGroupsRequest(); describeSecurityGroupsRequest.setSecurityGroupName(description.getSecurityGroupName()); @@ -95,8 +96,7 @@ public Void operate(List priorOutputs) { return null; } - private void buildIngressRule(IAcsClient client, String securityGroupId) - throws ClientException, ServerException { + private void buildIngressRule(IAcsClient client, String securityGroupId) throws ClientException { DescribeSecurityGroupAttributeRequest securityGroupAttributeRequest = new DescribeSecurityGroupAttributeRequest(); securityGroupAttributeRequest.setSecurityGroupId(securityGroupId); diff --git a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/model/AliCloudLoadBalancer.java b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/model/AliCloudLoadBalancer.java index 924c1d7edc9..2289fb150a5 100644 --- a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/model/AliCloudLoadBalancer.java +++ b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/model/AliCloudLoadBalancer.java @@ -48,6 +48,7 @@ public class AliCloudLoadBalancer implements LoadBalancer { Set securityGroups; Map labels = new HashMap<>(); + String loadBalancerType; public AliCloudLoadBalancer( String account, String region, String name, String vpcId, String loadBalancerId) { diff --git a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/model/AliCloudLoadBalancerType.java b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/model/AliCloudLoadBalancerType.java index ffab4f64c57..75318a22503 100644 --- a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/model/AliCloudLoadBalancerType.java +++ b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/model/AliCloudLoadBalancerType.java @@ -17,7 +17,13 @@ package com.netflix.spinnaker.clouddriver.alicloud.model; public enum AliCloudLoadBalancerType { - CLASSIC; + ALB, + CLB; + public final String ns; + + AliCloudLoadBalancerType() { + ns = name().toLowerCase(); + } public static AliCloudLoadBalancerType getByValue(String value) { for (AliCloudLoadBalancerType lbt : values()) { @@ -27,9 +33,4 @@ public static AliCloudLoadBalancerType getByValue(String value) { } return null; } - - @Override - public String toString() { - return this.name().toLowerCase(); - } } diff --git a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/provider/agent/AliCloudClusterCachingAgent.java b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/provider/agent/AliCloudClusterCachingAgent.java index 38ef7411a54..da72ac58d71 100644 --- a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/provider/agent/AliCloudClusterCachingAgent.java +++ b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/provider/agent/AliCloudClusterCachingAgent.java @@ -58,6 +58,7 @@ import com.netflix.spinnaker.clouddriver.alicloud.cache.Keys; import com.netflix.spinnaker.clouddriver.alicloud.common.CacheDataHelper; import com.netflix.spinnaker.clouddriver.alicloud.common.Sets; +import com.netflix.spinnaker.clouddriver.alicloud.model.AliCloudLoadBalancerType; import com.netflix.spinnaker.clouddriver.alicloud.provider.AliProvider; import com.netflix.spinnaker.clouddriver.alicloud.security.AliCloudCredentials; import com.netflix.spinnaker.clouddriver.cache.OnDemandAgent; @@ -338,27 +339,24 @@ public ClusterCachingBuilder( Optional.ofNullable(loadBalancerAttributes) .ifPresent( l -> { - loadBalancerKeys.addAll( - l.stream() - .map( - loadBalancerAttribute -> - (Keys.getLoadBalancerKey( - loadBalancerAttribute.getLoadBalancerName(), - account, - region, - loadBalancerAttribute.getVpcId()))) - .collect(Collectors.toSet())); + l.forEach( + loadBalancerAttribute -> { + loadBalancerKeys.add( + Keys.getLoadBalancerKey( + loadBalancerAttribute.getLoadBalancerName(), + account, + region, + loadBalancerAttribute.getVpcId(), + AliCloudLoadBalancerType.CLB.ns)); + }); }); Optional.ofNullable(scalingInstances) .ifPresent( instances -> { - instanceIds.addAll( - instances.stream() - .map( - scalingInstance -> - Keys.getInstanceKey( - scalingInstance.getInstanceId(), account, region)) - .collect(Collectors.toSet())); + instances.forEach( + scalingInstance -> + instanceIds.add( + Keys.getInstanceKey(scalingInstance.getInstanceId(), account, region))); }); } @@ -394,7 +392,8 @@ public CacheData buildServerGroupCacheData(ObjectMapper objectMapper) { attributes.put("scalingGroup", this.scalingGroup); attributes.put("region", this.region); attributes.put("name", this.scalingGroup.getScalingGroupName()); - if (this.scalingConfigurationsResponse.getScalingConfigurations().size() > 0) { + if (this.scalingConfigurationsResponse != null + && this.scalingConfigurationsResponse.getScalingConfigurations().size() > 0) { attributes.put( "launchConfigName", this.scalingConfigurationsResponse diff --git a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/provider/agent/AliCloudLoadBalancerCachingAgent.java b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/provider/agent/AliCloudLoadBalancerCachingAgent.java index a0c14abbe25..f8955c05743 100644 --- a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/provider/agent/AliCloudLoadBalancerCachingAgent.java +++ b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/provider/agent/AliCloudLoadBalancerCachingAgent.java @@ -44,6 +44,7 @@ import com.netflix.spinnaker.clouddriver.alicloud.AliCloudProvider; import com.netflix.spinnaker.clouddriver.alicloud.cache.Keys; import com.netflix.spinnaker.clouddriver.alicloud.model.AliCloudLoadBalancer; +import com.netflix.spinnaker.clouddriver.alicloud.model.AliCloudLoadBalancerType; import com.netflix.spinnaker.clouddriver.alicloud.provider.AliProvider; import com.netflix.spinnaker.clouddriver.alicloud.security.AliCloudClientProvider; import com.netflix.spinnaker.clouddriver.alicloud.security.AliCloudCredentials; @@ -320,7 +321,8 @@ public OnDemandResult handle(ProviderCache providerCache, Map { @@ -339,7 +341,8 @@ public OnDemandResult handle(ProviderCache providerCache, Map map = objectMapper.convertValue(loadBalancer, Map.class); map.put("account", this.account); + map.put("loadBalancerType", AliCloudLoadBalancerType.CLB.ns); Map attributeMap = objectMapper.convertValue(this.loadBalancerAttribute, Map.class); attributeMap.put("listenerPortsAndProtocal", this.listenerAttributes); @@ -422,7 +426,8 @@ public DefaultCacheData buildLoadBalancerCache(ObjectMapper objectMapper) { loadBalancer.getLoadBalancerName(), this.account, this.region, - loadBalancer.getVpcId()), + loadBalancer.getVpcId(), + AliCloudLoadBalancerType.CLB.ns), map, Maps.newHashMap()); } diff --git a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/provider/view/AliCloudLoadBalancerProvider.java b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/provider/view/AliCloudLoadBalancerProvider.java index bbcc2083f28..795b29889a8 100644 --- a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/provider/view/AliCloudLoadBalancerProvider.java +++ b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/provider/view/AliCloudLoadBalancerProvider.java @@ -25,6 +25,7 @@ import com.netflix.spinnaker.clouddriver.alicloud.common.HealthHelper; import com.netflix.spinnaker.clouddriver.alicloud.common.Sets; import com.netflix.spinnaker.clouddriver.alicloud.model.AliCloudLoadBalancer; +import com.netflix.spinnaker.clouddriver.alicloud.model.AliCloudLoadBalancerType; import com.netflix.spinnaker.clouddriver.model.LoadBalancerInstance; import com.netflix.spinnaker.clouddriver.model.LoadBalancerProvider; import com.netflix.spinnaker.clouddriver.model.LoadBalancerServerGroup; @@ -111,11 +112,10 @@ public Set getApplicationLoadBalancers(String applicationN @Override public List byAccountAndRegionAndName(String account, String region, String name) { List results = new ArrayList<>(); - String searchKey = Keys.getLoadBalancerKey(name, account, region, null) + "*"; + String searchKey = Keys.getLoadBalancerKey(name + "*", account, region, "*", "*"); Collection allLoadBalancerKeys = cacheView.filterIdentifiers(LOAD_BALANCERS.ns, searchKey); - Collection loadBalancers = - cacheView.getAll(LOAD_BALANCERS.ns, allLoadBalancerKeys, null); + Collection loadBalancers = cacheView.getAll(LOAD_BALANCERS.ns, allLoadBalancerKeys); for (CacheData loadBalancer : loadBalancers) { ResultDetails resultDetails = new ResultDetails(); Set serverGroups = new HashSet<>(); @@ -239,16 +239,17 @@ public void addServerGroupToLoadBalancer( private boolean isContainsLoadBalancer( AliCloudLoadBalancer loadBalancer, CacheData applicationServerGroup) { - String loadBalancerKey = + String clbLoadBalancerKey = Keys.getLoadBalancerKey( loadBalancer.getName(), loadBalancer.getAccount(), loadBalancer.getRegion(), - loadBalancer.getVpcId()); - return applicationServerGroup - .getRelationships() - .get(LOAD_BALANCERS.ns) - .contains(loadBalancerKey); + loadBalancer.getVpcId(), + AliCloudLoadBalancerType.CLB.ns); + Collection loadBalancerKeys = + applicationServerGroup.getRelationships().get(LOAD_BALANCERS.ns); + return AliCloudLoadBalancerType.ALB.ns.equalsIgnoreCase(loadBalancer.getLoadBalancerType()) + || loadBalancerKeys.contains(clbLoadBalancerKey); } class ResultDetails implements Details { diff --git a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/security/AliCloudCredentials.java b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/security/AliCloudCredentials.java index 437100d32b1..f5b9dd0f490 100644 --- a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/security/AliCloudCredentials.java +++ b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/security/AliCloudCredentials.java @@ -15,42 +15,42 @@ */ package com.netflix.spinnaker.clouddriver.alicloud.security; +import com.aliyuncs.auth.AlibabaCloudCredentials; +import com.aliyuncs.auth.AlibabaCloudCredentialsProvider; +import com.aliyuncs.exceptions.ClientException; import com.fasterxml.jackson.annotation.JsonIgnore; -import com.netflix.spinnaker.clouddriver.security.AccountCredentials; +import com.netflix.spinnaker.clouddriver.security.AbstractAccountCredentials; import java.util.List; +import lombok.EqualsAndHashCode; -public class AliCloudCredentials extends AbstractAccountCredentials { +@EqualsAndHashCode(callSuper = false) +public class AliCloudCredentials extends AbstractAccountCredentials { private static final String CLOUD_PROVIDER = "alicloud"; - private String name; + private final String name; + private final String environment; + private final String accountType; - private String accessKeyId; + private final List regions; - private String accessSecretKey; + private final List requiredGroupMembership; - private List regions; + private final AlibabaCloudCredentialsProvider credentialsProvider; - private List requiredGroupMembership; - - public void setName(String name) { + public AliCloudCredentials( + String name, + String environment, + String accountType, + List regions, + List requiredGroupMembership, + AlibabaCloudCredentialsProvider credentialsProvider) { this.name = name; - } - - public String getAccessKeyId() { - return accessKeyId; - } - - public void setAccessKeyId(String accessKeyId) { - this.accessKeyId = accessKeyId; - } - - public String getAccessSecretKey() { - return accessSecretKey; - } - - public void setAccessSecretKey(String accessSecretKey) { - this.accessSecretKey = accessSecretKey; + this.environment = environment; + this.accountType = accountType; + this.regions = regions; + this.requiredGroupMembership = requiredGroupMembership; + this.credentialsProvider = credentialsProvider; } @Override @@ -60,17 +60,22 @@ public String getName() { @Override public String getEnvironment() { - return null; + return environment; } @Override public String getAccountType() { - return null; + return accountType; } @Override @JsonIgnore - public AccountCredentials getCredentials() { + public AlibabaCloudCredentials getCredentials() { + try { + return credentialsProvider.getCredentials(); + } catch (ClientException e) { + e.printStackTrace(); + } return null; } @@ -88,7 +93,8 @@ public List getRegions() { return regions; } - public void setRegions(List regions) { - this.regions = regions; + @JsonIgnore + public AlibabaCloudCredentialsProvider getCredentialsProvider() { + return credentialsProvider; } } diff --git a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/security/AliCloudCredentialsInitializer.java b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/security/AliCloudCredentialsInitializer.java index 38c953ac465..8dd54f1d9b9 100644 --- a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/security/AliCloudCredentialsInitializer.java +++ b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/security/AliCloudCredentialsInitializer.java @@ -15,14 +15,22 @@ */ package com.netflix.spinnaker.clouddriver.alicloud.security; +import com.aliyuncs.auth.AlibabaCloudCredentialsProvider; +import com.aliyuncs.auth.BasicCredentials; +import com.aliyuncs.auth.EnvironmentVariableCredentialsProvider; +import com.aliyuncs.auth.STSAssumeRoleSessionCredentialsProvider; +import com.aliyuncs.auth.StaticCredentialsProvider; +import com.aliyuncs.profile.DefaultProfile; +import com.aliyuncs.utils.AuthUtils; import com.netflix.spinnaker.clouddriver.alicloud.security.config.AliCloudAccountConfig; +import com.netflix.spinnaker.clouddriver.alicloud.security.config.AliCloudAccountConfig.Account; import com.netflix.spinnaker.clouddriver.security.AccountCredentialsRepository; import com.netflix.spinnaker.clouddriver.security.CredentialsInitializerSynchronizable; import java.util.ArrayList; import java.util.List; +import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.config.ConfigurableBeanFactory; import org.springframework.boot.context.properties.ConfigurationProperties; -import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Scope; @@ -40,20 +48,61 @@ AliCloudAccountConfig aliCloudAccountConfig() { @Bean List synchronizeAliCloudAccounts( AliCloudAccountConfig aliCloudAccountConfig, - AccountCredentialsRepository accountCredentialsRepository, - ApplicationContext applicationContext) { + AccountCredentialsRepository accountCredentialsRepository) { List aliCloudCredentialsList = new ArrayList<>(); + + if (StringUtils.isNotBlank(aliCloudAccountConfig.getAccessKeyId()) + && StringUtils.isNotBlank(aliCloudAccountConfig.getAccessSecretKey())) { + AuthUtils.setEnvironmentAccessKeyId(aliCloudAccountConfig.getAccessKeyId()); + AuthUtils.setEnvironmentAccessKeySecret(aliCloudAccountConfig.getAccessSecretKey()); + } + aliCloudAccountConfig.getAccounts().stream() .forEach( account -> { - AliCloudCredentials aliCloudCredentials = new AliCloudCredentials(); - aliCloudCredentials.setName(account.getName()); - aliCloudCredentials.setAccessSecretKey(account.getAccessSecretKey()); - aliCloudCredentials.setAccessKeyId(account.getAccessKeyId()); - aliCloudCredentials.setRegions(account.getRegions()); + AlibabaCloudCredentialsProvider provider = + buildProvider(account, aliCloudAccountConfig.getDefaultRegion()); + + AliCloudCredentials aliCloudCredentials = + new AliCloudCredentials( + account.getName(), + account.getEnvironment(), + account.getAccountType(), + account.getRegions(), + account.getRequiredGroupMembership(), + provider); accountCredentialsRepository.save(account.getName(), aliCloudCredentials); aliCloudCredentialsList.add(aliCloudCredentials); }); return aliCloudCredentialsList; } + + private AlibabaCloudCredentialsProvider buildProvider(Account account, String defaultRegion) { + AlibabaCloudCredentialsProvider provider = new EnvironmentVariableCredentialsProvider(); + + if (StringUtils.isNotBlank(account.getAssumeRole())) { + + String roleArn = + String.format("acs:ram::%s:%s", account.getAccountId(), account.getAssumeRole()); + + if (StringUtils.isNotBlank(account.getAccessKeyId()) + && StringUtils.isNotBlank(account.getAccessSecretKey())) { + return new STSAssumeRoleSessionCredentialsProvider( + new StaticCredentialsProvider( + new BasicCredentials(account.getAccessKeyId(), account.getAccessSecretKey())), + roleArn, + DefaultProfile.getProfile(defaultRegion)); + } else { + return new STSAssumeRoleSessionCredentialsProvider( + provider, roleArn, DefaultProfile.getProfile(defaultRegion)); + } + } else { + if (StringUtils.isNotBlank(account.getAccessKeyId()) + && StringUtils.isNotBlank(account.getAccessSecretKey())) { + return new StaticCredentialsProvider( + new BasicCredentials(account.getAccessKeyId(), account.getAccessSecretKey())); + } + } + return provider; + } } diff --git a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/security/config/AliCloudAccountConfig.java b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/security/config/AliCloudAccountConfig.java index 08c77b692a9..757dc709892 100644 --- a/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/security/config/AliCloudAccountConfig.java +++ b/clouddriver-alicloud/src/main/java/com/netflix/spinnaker/clouddriver/alicloud/security/config/AliCloudAccountConfig.java @@ -17,6 +17,8 @@ import java.util.List; import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.EqualsAndHashCode.Include; /** * A mutable credentials configurations structure suitable for transformation into concrete @@ -26,16 +28,21 @@ public class AliCloudAccountConfig { private List accounts; + private String accessKeyId; + private String accessSecretKey; + private String defaultRegion; @Data + @EqualsAndHashCode(onlyExplicitlyIncluded = true) public static class Account { - - private String name; - - private String accessKeyId; - - private String accessSecretKey; - - private List regions; + @Include private String name; + @Include private String environment; + @Include private String accountType; + @Include private String accessKeyId; + @Include private String accessSecretKey; + @Include private String accountId; + @Include private String assumeRole; + @Include private List regions; + List requiredGroupMembership; } } diff --git a/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/cache/KeysTest.java b/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/cache/KeysTest.java index 0ff7dab080d..5d0a888e705 100644 --- a/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/cache/KeysTest.java +++ b/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/cache/KeysTest.java @@ -15,7 +15,7 @@ */ package com.netflix.spinnaker.clouddriver.alicloud.cache; -import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertEquals; import org.junit.Test; @@ -24,39 +24,32 @@ public class KeysTest { static final String ACCOUNT = "test-account"; static final String REGION = "cn-test"; - @Test - public void testGetLoadBalancerKey() { - String key = "alicloud:loadBalancers:test-account:cn-test:test-loadBalancer"; - String loadBalancerKey = Keys.getLoadBalancerKey("test-loadBalancer", ACCOUNT, REGION, null); - assertTrue(key.equals(loadBalancerKey)); - } - @Test public void testGetSubnetKey() { String key = "alicloud:subnets:test-account:cn-test:test-vswitchId"; String subnetKey = Keys.getSubnetKey("test-vswitchId", REGION, ACCOUNT); - assertTrue(key.equals(subnetKey)); + assertEquals(key, subnetKey); } @Test public void testGetImageKey() { String key = "alicloud:images:test-account:cn-test:test-imageId"; String imageKey = Keys.getImageKey("test-imageId", ACCOUNT, REGION); - assertTrue(key.equals(imageKey)); + assertEquals(key, imageKey); } @Test public void testGetNamedImageKey() { String key = "alicloud:namedImages:test-account:test-imageName"; String namedImageKey = Keys.getNamedImageKey(ACCOUNT, "test-imageName"); - assertTrue(key.equals(namedImageKey)); + assertEquals(key, namedImageKey); } @Test public void testGetInstanceTypeKey() { String key = "alicloud:instanceTypes:test-account:cn-test:test-zoneId"; String instanceTypeKey = Keys.getInstanceTypeKey(ACCOUNT, REGION, "test-zoneId"); - assertTrue(key.equals(instanceTypeKey)); + assertEquals(key, instanceTypeKey); } @Test @@ -66,48 +59,48 @@ public void testGetSecurityGroupKey() { String securityGroupKey = Keys.getSecurityGroupKey( "test-SecurityGroupName", "test-SecurityGroupId", REGION, ACCOUNT, null); - assertTrue(key.equals(securityGroupKey)); + assertEquals(key, securityGroupKey); } @Test public void testGetKeyPairKey() { String key = "alicloud:aliCloudKeyPairs:test-KeyPair:test-account:cn-test"; String keyPairKey = Keys.getKeyPairKey("test-KeyPair", REGION, ACCOUNT); - assertTrue(key.equals(keyPairKey)); + assertEquals(key, keyPairKey); } @Test public void testGetServerGroupKey() { String key = "alicloud:serverGroups:Spin63-test-ali:test-account:cn-test:Spin63-test-ali"; String serverGroupKey = Keys.getServerGroupKey("Spin63-test-ali", ACCOUNT, REGION); - assertTrue(key.equals(serverGroupKey)); + assertEquals(key, serverGroupKey); } @Test public void testGetApplicationKey() { String key = "alicloud:applications:test-application"; String applicationKey = Keys.getApplicationKey("test-Application"); - assertTrue(key.equals(applicationKey)); + assertEquals(key, applicationKey); } @Test public void testGetClusterKey() { String key = "alicloud:clusters:test-application:test-account:test-Cluster"; String clusterKey = Keys.getClusterKey("test-Cluster", "test-Application", ACCOUNT); - assertTrue(key.equals(clusterKey)); + assertEquals(key, clusterKey); } @Test public void testGetLaunchConfigKey() { String key = "alicloud:launchConfigs:test-account:cn-test:test-LaunchConfigName"; String launchConfigKey = Keys.getLaunchConfigKey("test-LaunchConfigName", ACCOUNT, REGION); - assertTrue(key.equals(launchConfigKey)); + assertEquals(key, launchConfigKey); } @Test public void testGetInstanceKey() { String key = "alicloud:instances:test-account:cn-test:test-instanceId"; String instanceKey = Keys.getInstanceKey("test-instanceId", ACCOUNT, REGION); - assertTrue(key.equals(instanceKey)); + assertEquals(key, instanceKey); } } diff --git a/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/controllers/AliCloudImageControllerTest.java b/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/controllers/AliCloudImageControllerTest.java index cdb264d9cfa..e7684f28cc8 100644 --- a/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/controllers/AliCloudImageControllerTest.java +++ b/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/controllers/AliCloudImageControllerTest.java @@ -5,7 +5,7 @@ * 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 + * 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, @@ -15,7 +15,7 @@ */ package com.netflix.spinnaker.clouddriver.alicloud.controllers; -import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertEquals; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.mock; @@ -26,7 +26,12 @@ import com.netflix.spinnaker.cats.cache.DefaultCacheData; import com.netflix.spinnaker.clouddriver.alicloud.controllers.AliCloudImageController.Image; import com.netflix.spinnaker.clouddriver.alicloud.controllers.AliCloudImageController.LookupOptions; -import java.util.*; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Enumeration; +import java.util.HashMap; +import java.util.List; +import java.util.Map; import javax.servlet.http.HttpServletRequest; import org.junit.Before; import org.junit.Test; @@ -54,7 +59,7 @@ public void testBefore() { public void testList() { AliCloudImageController controller = new AliCloudImageController(cacheView); List list = controller.list(lookupOptions, request); - assertTrue(list.size() == 2); + assertEquals(1, list.size()); } private class FilterAnswer implements Answer> { diff --git a/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/controllers/AliCloudScalingActivitiesControllerTest.java b/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/controllers/AliCloudScalingActivitiesControllerTest.java index 010e94736b0..c98c5d574b9 100644 --- a/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/controllers/AliCloudScalingActivitiesControllerTest.java +++ b/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/controllers/AliCloudScalingActivitiesControllerTest.java @@ -5,7 +5,7 @@ * 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 + * 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, diff --git a/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/converters/CommonConverter.java b/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/converters/CommonConverter.java index 6a4171a2b53..4689191eba9 100644 --- a/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/converters/CommonConverter.java +++ b/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/converters/CommonConverter.java @@ -5,7 +5,7 @@ * 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 + * 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, @@ -15,11 +15,13 @@ */ package com.netflix.spinnaker.clouddriver.alicloud.deploy.converters; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import com.aliyuncs.IAcsClient; +import com.aliyuncs.auth.AlibabaCloudCredentials; import com.netflix.spinnaker.clouddriver.alicloud.common.ClientFactory; import com.netflix.spinnaker.clouddriver.alicloud.security.AliCloudCredentials; import com.netflix.spinnaker.clouddriver.model.ClusterProvider; @@ -40,9 +42,20 @@ public class CommonConverter { static { when(credentials.getName()).thenReturn(ACCOUNT); - when(credentials.getAccessKeyId()).thenReturn("test-ak"); - when(credentials.getAccessSecretKey()).thenReturn("test-sk"); - when(clientFactory.createClient(anyString(), anyString(), anyString())).thenReturn(client); + when(credentials.getCredentials()) + .thenReturn( + new AlibabaCloudCredentials() { + @Override + public String getAccessKeyId() { + return "test-ak"; + } + + @Override + public String getAccessKeySecret() { + return "test-sk"; + } + }); + when(clientFactory.createClient(anyString(), any())).thenReturn(client); when(accountCredentialsProvider.getCredentials(anyString())).thenReturn(credentials); } } diff --git a/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/converters/CreateAliCloudServerGroupAtomicOperationConverterTest.java b/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/converters/CreateAliCloudServerGroupAtomicOperationConverterTest.java new file mode 100644 index 00000000000..4ed947f4495 --- /dev/null +++ b/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/converters/CreateAliCloudServerGroupAtomicOperationConverterTest.java @@ -0,0 +1,59 @@ +/* + * 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.clouddriver.alicloud.deploy.converters; + +import static org.junit.Assert.assertTrue; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.netflix.spinnaker.clouddriver.alicloud.deploy.description.BasicAliCloudDeployDescription; +import com.netflix.spinnaker.clouddriver.alicloud.deploy.ops.CreateAliCloudServerGroupAtomicOperation; +import com.netflix.spinnaker.clouddriver.orchestration.AtomicOperation; +import java.util.HashMap; +import java.util.Map; +import org.junit.Before; +import org.junit.Test; + +public class CreateAliCloudServerGroupAtomicOperationConverterTest extends CommonConverter { + + CreateAliCloudServerGroupAtomicOperationConverter converter = + new CreateAliCloudServerGroupAtomicOperationConverter(clientFactory, clusterProviders); + + @Before + public void testBefore() { + converter.setObjectMapper(new ObjectMapper()); + converter.setAccountCredentialsProvider(accountCredentialsProvider); + } + + @Test + public void testConvertOperation() { + AtomicOperation atomicOperation = converter.convertOperation(buildDescription()); + assertTrue(atomicOperation instanceof CreateAliCloudServerGroupAtomicOperation); + } + + @Test + public void testConvertDescription() { + BasicAliCloudDeployDescription basicAliCloudDeployDescription = + converter.convertDescription(buildDescription()); + assertTrue(basicAliCloudDeployDescription instanceof BasicAliCloudDeployDescription); + } + + private Map buildDescription() { + Map description = new HashMap<>(); + description.put("region", REGION); + description.put("credentials", ACCOUNT); + return description; + } +} diff --git a/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/converters/DeleteAliCloudLoadBalancerAtomicOperationConverterTest.java b/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/converters/DeleteAliCloudLoadBalancerAtomicOperationConverterTest.java index e1fa96468fa..d4e791a6949 100644 --- a/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/converters/DeleteAliCloudLoadBalancerAtomicOperationConverterTest.java +++ b/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/converters/DeleteAliCloudLoadBalancerAtomicOperationConverterTest.java @@ -5,7 +5,7 @@ * 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 + * 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, diff --git a/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/converters/DeleteAliCloudSecurityGroupAtomicOperationConverterTest.java b/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/converters/DeleteAliCloudSecurityGroupAtomicOperationConverterTest.java index 6a77918bdf8..dd8b528d6de 100644 --- a/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/converters/DeleteAliCloudSecurityGroupAtomicOperationConverterTest.java +++ b/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/converters/DeleteAliCloudSecurityGroupAtomicOperationConverterTest.java @@ -5,7 +5,7 @@ * 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 + * 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, diff --git a/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/converters/DestroyAliCloudServerGroupAtomicOperationConverterTest.java b/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/converters/DestroyAliCloudServerGroupAtomicOperationConverterTest.java new file mode 100644 index 00000000000..beeace62db7 --- /dev/null +++ b/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/converters/DestroyAliCloudServerGroupAtomicOperationConverterTest.java @@ -0,0 +1,60 @@ +/* + * 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.clouddriver.alicloud.deploy.converters; + +import static org.junit.Assert.assertTrue; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.netflix.spinnaker.clouddriver.alicloud.deploy.description.DestroyAliCloudServerGroupDescription; +import com.netflix.spinnaker.clouddriver.alicloud.deploy.ops.DestroyAliCloudServerGroupAtomicOperation; +import com.netflix.spinnaker.clouddriver.orchestration.AtomicOperation; +import java.util.HashMap; +import java.util.Map; +import org.junit.Before; +import org.junit.Test; + +public class DestroyAliCloudServerGroupAtomicOperationConverterTest extends CommonConverter { + + DestroyAliCloudServerGroupAtomicOperationConverter converter = + new DestroyAliCloudServerGroupAtomicOperationConverter(clientFactory); + + @Before + public void testBefore() { + converter.setObjectMapper(new ObjectMapper()); + converter.setAccountCredentialsProvider(accountCredentialsProvider); + } + + @Test + public void testConvertOperation() { + AtomicOperation atomicOperation = converter.convertOperation(buildDescription()); + assertTrue(atomicOperation instanceof DestroyAliCloudServerGroupAtomicOperation); + } + + @Test + public void testConvertDescription() { + DestroyAliCloudServerGroupDescription destroyAliCloudServerGroupDescription = + converter.convertDescription(buildDescription()); + assertTrue( + destroyAliCloudServerGroupDescription instanceof DestroyAliCloudServerGroupDescription); + } + + private Map buildDescription() { + Map description = new HashMap<>(); + description.put("region", REGION); + description.put("credentials", ACCOUNT); + return description; + } +} diff --git a/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/converters/DisableAliCloudServerGroupAtomicOperationConverterTest.java b/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/converters/DisableAliCloudServerGroupAtomicOperationConverterTest.java new file mode 100644 index 00000000000..74d801ba77f --- /dev/null +++ b/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/converters/DisableAliCloudServerGroupAtomicOperationConverterTest.java @@ -0,0 +1,60 @@ +/* + * 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.clouddriver.alicloud.deploy.converters; + +import static org.junit.Assert.assertTrue; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.netflix.spinnaker.clouddriver.alicloud.deploy.description.DisableAliCloudServerGroupDescription; +import com.netflix.spinnaker.clouddriver.alicloud.deploy.ops.DisableAliCloudServerGroupAtomicOperation; +import com.netflix.spinnaker.clouddriver.orchestration.AtomicOperation; +import java.util.HashMap; +import java.util.Map; +import org.junit.Before; +import org.junit.Test; + +public class DisableAliCloudServerGroupAtomicOperationConverterTest extends CommonConverter { + + DisableAliCloudServerGroupAtomicOperationConverter converter = + new DisableAliCloudServerGroupAtomicOperationConverter(clientFactory); + + @Before + public void testBefore() { + converter.setObjectMapper(new ObjectMapper()); + converter.setAccountCredentialsProvider(accountCredentialsProvider); + } + + @Test + public void testConvertOperation() { + AtomicOperation atomicOperation = converter.convertOperation(buildDescription()); + assertTrue(atomicOperation instanceof DisableAliCloudServerGroupAtomicOperation); + } + + @Test + public void testConvertDescription() { + DisableAliCloudServerGroupDescription disableAliCloudServerGroupDescription = + converter.convertDescription(buildDescription()); + assertTrue( + disableAliCloudServerGroupDescription instanceof DisableAliCloudServerGroupDescription); + } + + private Map buildDescription() { + Map description = new HashMap<>(); + description.put("region", REGION); + description.put("credentials", ACCOUNT); + return description; + } +} diff --git a/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/converters/EnableAliCloudServerGroupAtomicOperationConverterTest.java b/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/converters/EnableAliCloudServerGroupAtomicOperationConverterTest.java new file mode 100644 index 00000000000..88fb82b94b5 --- /dev/null +++ b/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/converters/EnableAliCloudServerGroupAtomicOperationConverterTest.java @@ -0,0 +1,60 @@ +/* + * 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.clouddriver.alicloud.deploy.converters; + +import static org.junit.Assert.assertTrue; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.netflix.spinnaker.clouddriver.alicloud.deploy.description.EnableAliCloudServerGroupDescription; +import com.netflix.spinnaker.clouddriver.alicloud.deploy.ops.EnableAliCloudServerGroupAtomicOperation; +import com.netflix.spinnaker.clouddriver.orchestration.AtomicOperation; +import java.util.HashMap; +import java.util.Map; +import org.junit.Before; +import org.junit.Test; + +public class EnableAliCloudServerGroupAtomicOperationConverterTest extends CommonConverter { + + EnableAliCloudServerGroupAtomicOperationConverter converter = + new EnableAliCloudServerGroupAtomicOperationConverter(clientFactory); + + @Before + public void testBefore() { + converter.setObjectMapper(new ObjectMapper()); + converter.setAccountCredentialsProvider(accountCredentialsProvider); + } + + @Test + public void testConvertOperation() { + AtomicOperation atomicOperation = converter.convertOperation(buildDescription()); + assertTrue(atomicOperation instanceof EnableAliCloudServerGroupAtomicOperation); + } + + @Test + public void testConvertDescription() { + EnableAliCloudServerGroupDescription enableAliCloudServerGroupDescription = + converter.convertDescription(buildDescription()); + assertTrue( + enableAliCloudServerGroupDescription instanceof EnableAliCloudServerGroupDescription); + } + + private Map buildDescription() { + Map description = new HashMap<>(); + description.put("region", REGION); + description.put("credentials", ACCOUNT); + return description; + } +} diff --git a/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/converters/ModifyScalingGroupAtomicOperationConverterTest.java b/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/converters/ModifyScalingGroupAtomicOperationConverterTest.java new file mode 100644 index 00000000000..f47ebd13140 --- /dev/null +++ b/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/converters/ModifyScalingGroupAtomicOperationConverterTest.java @@ -0,0 +1,59 @@ +/* + * 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.clouddriver.alicloud.deploy.converters; + +import static org.junit.Assert.assertTrue; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.netflix.spinnaker.clouddriver.alicloud.deploy.description.ModifyScalingGroupDescription; +import com.netflix.spinnaker.clouddriver.alicloud.deploy.ops.ModifyScalingGroupAtomicOperation; +import com.netflix.spinnaker.clouddriver.orchestration.AtomicOperation; +import java.util.HashMap; +import java.util.Map; +import org.junit.Before; +import org.junit.Test; + +public class ModifyScalingGroupAtomicOperationConverterTest extends CommonConverter { + + ModifyScalingGroupAtomicOperationConverter converter = + new ModifyScalingGroupAtomicOperationConverter(clientFactory); + + @Before + public void testBefore() { + converter.setObjectMapper(new ObjectMapper()); + converter.setAccountCredentialsProvider(accountCredentialsProvider); + } + + @Test + public void testConvertOperation() { + AtomicOperation atomicOperation = converter.convertOperation(buildDescription()); + assertTrue(atomicOperation instanceof ModifyScalingGroupAtomicOperation); + } + + @Test + public void testConvertDescription() { + ModifyScalingGroupDescription modifyScalingGroupDescription = + converter.convertDescription(buildDescription()); + assertTrue(modifyScalingGroupDescription instanceof ModifyScalingGroupDescription); + } + + private Map buildDescription() { + Map description = new HashMap<>(); + description.put("region", REGION); + description.put("credentials", ACCOUNT); + return description; + } +} diff --git a/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/converters/ResizeAliCloudServerGroupAtomicOperationConverterTest.java b/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/converters/ResizeAliCloudServerGroupAtomicOperationConverterTest.java new file mode 100644 index 00000000000..2815a9a8d5f --- /dev/null +++ b/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/converters/ResizeAliCloudServerGroupAtomicOperationConverterTest.java @@ -0,0 +1,60 @@ +/* + * 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.clouddriver.alicloud.deploy.converters; + +import static org.junit.Assert.assertTrue; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.netflix.spinnaker.clouddriver.alicloud.deploy.description.ResizeAliCloudServerGroupDescription; +import com.netflix.spinnaker.clouddriver.alicloud.deploy.ops.ResizeAliCloudServerGroupAtomicOperation; +import com.netflix.spinnaker.clouddriver.orchestration.AtomicOperation; +import java.util.HashMap; +import java.util.Map; +import org.junit.Before; +import org.junit.Test; + +public class ResizeAliCloudServerGroupAtomicOperationConverterTest extends CommonConverter { + + ResizeAliCloudServerGroupAtomicOperationConverter converter = + new ResizeAliCloudServerGroupAtomicOperationConverter(clientFactory); + + @Before + public void testBefore() { + converter.setObjectMapper(new ObjectMapper()); + converter.setAccountCredentialsProvider(accountCredentialsProvider); + } + + @Test + public void testConvertOperation() { + AtomicOperation atomicOperation = converter.convertOperation(buildDescription()); + assertTrue(atomicOperation instanceof ResizeAliCloudServerGroupAtomicOperation); + } + + @Test + public void testConvertDescription() { + ResizeAliCloudServerGroupDescription resizeAliCloudServerGroupDescription = + converter.convertDescription(buildDescription()); + assertTrue( + resizeAliCloudServerGroupDescription instanceof ResizeAliCloudServerGroupDescription); + } + + private Map buildDescription() { + Map description = new HashMap<>(); + description.put("region", REGION); + description.put("credentials", ACCOUNT); + return description; + } +} diff --git a/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/converters/UpdateAliCloudLaunchConfigAtomicOperationConverterTest.java b/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/converters/UpdateAliCloudLaunchConfigAtomicOperationConverterTest.java new file mode 100644 index 00000000000..576a717a5b7 --- /dev/null +++ b/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/converters/UpdateAliCloudLaunchConfigAtomicOperationConverterTest.java @@ -0,0 +1,60 @@ +/* + * 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.clouddriver.alicloud.deploy.converters; + +import static org.junit.Assert.assertTrue; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.netflix.spinnaker.clouddriver.alicloud.deploy.description.UpdateAliCloudLaunchConfigDescription; +import com.netflix.spinnaker.clouddriver.alicloud.deploy.ops.UpdateAliCloudLaunchConfigAtomicOperation; +import com.netflix.spinnaker.clouddriver.orchestration.AtomicOperation; +import java.util.HashMap; +import java.util.Map; +import org.junit.Before; +import org.junit.Test; + +public class UpdateAliCloudLaunchConfigAtomicOperationConverterTest extends CommonConverter { + + UpdateAliCloudLaunchConfigAtomicOperationConverter converter = + new UpdateAliCloudLaunchConfigAtomicOperationConverter(clientFactory); + + @Before + public void testBefore() { + converter.setObjectMapper(new ObjectMapper()); + converter.setAccountCredentialsProvider(accountCredentialsProvider); + } + + @Test + public void testConvertOperation() { + AtomicOperation atomicOperation = converter.convertOperation(buildDescription()); + assertTrue(atomicOperation instanceof UpdateAliCloudLaunchConfigAtomicOperation); + } + + @Test + public void testConvertDescription() { + UpdateAliCloudLaunchConfigDescription updateAliCloudLaunchConfigDescription = + converter.convertDescription(buildDescription()); + assertTrue( + updateAliCloudLaunchConfigDescription instanceof UpdateAliCloudLaunchConfigDescription); + } + + private Map buildDescription() { + Map description = new HashMap<>(); + description.put("region", REGION); + description.put("credentials", ACCOUNT); + return description; + } +} diff --git a/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/converters/UpsertAliCloudLoadBalancerAtomicOperationConverterTest.java b/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/converters/UpsertAliCloudLoadBalancerAtomicOperationConverterTest.java index 36320d0294f..272e5cd3c70 100644 --- a/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/converters/UpsertAliCloudLoadBalancerAtomicOperationConverterTest.java +++ b/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/converters/UpsertAliCloudLoadBalancerAtomicOperationConverterTest.java @@ -5,7 +5,7 @@ * 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 + * 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, diff --git a/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/converters/UpsertAliCloudSecurityGroupAtomicOperationConverterTest.java b/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/converters/UpsertAliCloudSecurityGroupAtomicOperationConverterTest.java index 950097eb72b..6d3d5fce8fe 100644 --- a/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/converters/UpsertAliCloudSecurityGroupAtomicOperationConverterTest.java +++ b/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/converters/UpsertAliCloudSecurityGroupAtomicOperationConverterTest.java @@ -5,7 +5,7 @@ * 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 + * 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, diff --git a/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/CommonAtomicOperation.java b/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/CommonAtomicOperation.java index 0b343e8d292..0beb7b3096b 100644 --- a/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/CommonAtomicOperation.java +++ b/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/CommonAtomicOperation.java @@ -5,7 +5,7 @@ * 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 + * 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, @@ -15,11 +15,13 @@ */ package com.netflix.spinnaker.clouddriver.alicloud.deploy.ops; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import com.aliyuncs.IAcsClient; +import com.aliyuncs.auth.AlibabaCloudCredentials; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; @@ -43,9 +45,20 @@ public class CommonAtomicOperation { static { when(credentials.getName()).thenReturn(ACCOUNT); - when(credentials.getAccessKeyId()).thenReturn("test-ak"); - when(credentials.getAccessSecretKey()).thenReturn("test-sk"); - when(clientFactory.createClient(anyString(), anyString(), anyString())).thenReturn(client); + when(credentials.getCredentials()) + .thenReturn( + new AlibabaCloudCredentials() { + @Override + public String getAccessKeyId() { + return "test-ak"; + } + + @Override + public String getAccessKeySecret() { + return "test-sk"; + } + }); + when(clientFactory.createClient(anyString(), any())).thenReturn(client); } @Subject diff --git a/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/CreateAliCloudServerGroupAtomicOperationTest.java b/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/CreateAliCloudServerGroupAtomicOperationTest.java new file mode 100644 index 00000000000..0338c98d874 --- /dev/null +++ b/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/CreateAliCloudServerGroupAtomicOperationTest.java @@ -0,0 +1,94 @@ +/* + * 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.clouddriver.alicloud.deploy.ops; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; + +import com.aliyuncs.ess.model.v20140828.CreateScalingConfigurationRequest; +import com.aliyuncs.ess.model.v20140828.CreateScalingConfigurationResponse; +import com.aliyuncs.ess.model.v20140828.CreateScalingGroupResponse; +import com.aliyuncs.ess.model.v20140828.EnableScalingGroupResponse; +import com.aliyuncs.exceptions.ClientException; +import com.netflix.spinnaker.clouddriver.alicloud.deploy.description.BasicAliCloudDeployDescription; +import java.util.ArrayList; +import java.util.List; +import org.junit.Before; +import org.junit.Test; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; + +public class CreateAliCloudServerGroupAtomicOperationTest extends CommonAtomicOperation { + + @Before + public void testBefore() throws ClientException { + when(client.getAcsResponse(any())) + .thenAnswer(new CreateScalingGroupAnswer()) + .thenAnswer(new CreateScalingConfigurationAnswer()) + .thenAnswer(new EnableScalingGroupAnswer()); + } + + @Test + public void testOperate() { + CreateAliCloudServerGroupAtomicOperation operation = + new CreateAliCloudServerGroupAtomicOperation( + buildDescription(), objectMapper, clientFactory, clusterProviders); + operation.operate(priorOutputs); + } + + private BasicAliCloudDeployDescription buildDescription() { + BasicAliCloudDeployDescription description = new BasicAliCloudDeployDescription(); + description.setRegion(REGION); + description.setCredentials(credentials); + description.setScalingGroupName("test-Name"); + List scalingConfigurations = new ArrayList<>(); + CreateScalingConfigurationRequest request = new CreateScalingConfigurationRequest(); + request.setScalingConfigurationName("test-ScalingConfigurationName"); + scalingConfigurations.add(request); + description.setScalingConfigurations(scalingConfigurations); + description.setApplication("spin63"); + description.setStack("test"); + description.setFreeFormDetails("test"); + return description; + } + + private class CreateScalingGroupAnswer implements Answer { + @Override + public CreateScalingGroupResponse answer(InvocationOnMock invocation) throws Throwable { + CreateScalingGroupResponse response = new CreateScalingGroupResponse(); + response.setScalingGroupId("test-ScalingGroupId"); + return response; + } + } + + private class CreateScalingConfigurationAnswer + implements Answer { + @Override + public CreateScalingConfigurationResponse answer(InvocationOnMock invocation) throws Throwable { + CreateScalingConfigurationResponse response = new CreateScalingConfigurationResponse(); + response.setScalingConfigurationId("test-ScalingConfigurationId"); + return response; + } + } + + private class EnableScalingGroupAnswer implements Answer { + @Override + public EnableScalingGroupResponse answer(InvocationOnMock invocation) throws Throwable { + EnableScalingGroupResponse response = new EnableScalingGroupResponse(); + return response; + } + } +} diff --git a/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/DeleteAliCloudLoadBalancerClassicAtomicOperationTest.java b/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/DeleteAliCloudLoadBalancerClassicAtomicOperationTest.java index aeb696e85bb..ada93533419 100644 --- a/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/DeleteAliCloudLoadBalancerClassicAtomicOperationTest.java +++ b/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/DeleteAliCloudLoadBalancerClassicAtomicOperationTest.java @@ -5,7 +5,7 @@ * 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 + * 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, diff --git a/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/DeleteAliCloudSecurityGroupAtomicOperationTest.java b/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/DeleteAliCloudSecurityGroupAtomicOperationTest.java index 3436e2395ae..eeb3f1fea2e 100644 --- a/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/DeleteAliCloudSecurityGroupAtomicOperationTest.java +++ b/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/DeleteAliCloudSecurityGroupAtomicOperationTest.java @@ -5,7 +5,7 @@ * 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 + * 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, diff --git a/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/DestroyAliCloudServerGroupAtomicOperationTest.java b/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/DestroyAliCloudServerGroupAtomicOperationTest.java new file mode 100644 index 00000000000..67a17cbe831 --- /dev/null +++ b/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/DestroyAliCloudServerGroupAtomicOperationTest.java @@ -0,0 +1,79 @@ +/* + * 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.clouddriver.alicloud.deploy.ops; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; + +import com.aliyuncs.ess.model.v20140828.DeleteScalingGroupResponse; +import com.aliyuncs.ess.model.v20140828.DescribeScalingGroupsResponse; +import com.aliyuncs.ess.model.v20140828.DescribeScalingGroupsResponse.ScalingGroup; +import com.aliyuncs.exceptions.ClientException; +import com.netflix.spinnaker.clouddriver.alicloud.deploy.description.DestroyAliCloudServerGroupDescription; +import java.util.ArrayList; +import java.util.List; +import org.junit.Before; +import org.junit.Test; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; + +public class DestroyAliCloudServerGroupAtomicOperationTest extends CommonAtomicOperation { + + @Before + public void testBefore() throws ClientException { + when(client.getAcsResponse(any())) + .thenAnswer(new DescribeScalingGroupsAnswer()) + .thenAnswer(new DeleteScalingGroupAnswer()); + } + + @Test + public void testOperate() { + DestroyAliCloudServerGroupAtomicOperation operation = + new DestroyAliCloudServerGroupAtomicOperation(buildDescription(), clientFactory); + operation.operate(priorOutputs); + } + + private DestroyAliCloudServerGroupDescription buildDescription() { + DestroyAliCloudServerGroupDescription description = new DestroyAliCloudServerGroupDescription(); + description.setRegion(REGION); + description.setCredentials(credentials); + description.setServerGroupName("test-ServerGroupName"); + return description; + } + + private class DescribeScalingGroupsAnswer implements Answer { + @Override + public DescribeScalingGroupsResponse answer(InvocationOnMock invocation) throws Throwable { + DescribeScalingGroupsResponse response = new DescribeScalingGroupsResponse(); + List scalingGroups = new ArrayList<>(); + DescribeScalingGroupsResponse.ScalingGroup scalingGroup = + new DescribeScalingGroupsResponse.ScalingGroup(); + scalingGroup.setScalingGroupId("test-ID"); + scalingGroup.setLifecycleState("Active"); + scalingGroups.add(scalingGroup); + response.setScalingGroups(scalingGroups); + return response; + } + } + + private class DeleteScalingGroupAnswer implements Answer { + @Override + public DeleteScalingGroupResponse answer(InvocationOnMock invocation) throws Throwable { + DeleteScalingGroupResponse response = new DeleteScalingGroupResponse(); + return response; + } + } +} diff --git a/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/DisableAliCloudServerGroupAtomicOperationTest.java b/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/DisableAliCloudServerGroupAtomicOperationTest.java new file mode 100644 index 00000000000..619a1febd87 --- /dev/null +++ b/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/DisableAliCloudServerGroupAtomicOperationTest.java @@ -0,0 +1,77 @@ +/* + * 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.clouddriver.alicloud.deploy.ops; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; + +import com.aliyuncs.ess.model.v20140828.DescribeScalingGroupsResponse; +import com.aliyuncs.ess.model.v20140828.DescribeScalingGroupsResponse.ScalingGroup; +import com.aliyuncs.ess.model.v20140828.DescribeScalingInstancesResponse; +import com.aliyuncs.exceptions.ClientException; +import com.netflix.spinnaker.clouddriver.alicloud.deploy.description.DisableAliCloudServerGroupDescription; +import java.util.ArrayList; +import java.util.List; +import org.junit.Before; +import org.junit.Test; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; + +public class DisableAliCloudServerGroupAtomicOperationTest extends CommonAtomicOperation { + @Before + public void testBefore() throws ClientException { + when(client.getAcsResponse(any())) + .thenAnswer(new DescribeScalingGroupsAnswer()) + .thenAnswer(new DescribeScalingInstancesAnswer()); + } + + @Test + public void testOperate() { + DisableAliCloudServerGroupAtomicOperation operation = + new DisableAliCloudServerGroupAtomicOperation(buildDescription(), clientFactory); + operation.operate(priorOutputs); + } + + private DisableAliCloudServerGroupDescription buildDescription() { + DisableAliCloudServerGroupDescription description = new DisableAliCloudServerGroupDescription(); + description.setRegion(REGION); + description.setCredentials(credentials); + return description; + } + + private class DescribeScalingGroupsAnswer implements Answer { + @Override + public DescribeScalingGroupsResponse answer(InvocationOnMock invocation) throws Throwable { + DescribeScalingGroupsResponse response = new DescribeScalingGroupsResponse(); + List scalingGroups = new ArrayList<>(); + DescribeScalingGroupsResponse.ScalingGroup scalingGroup = + new DescribeScalingGroupsResponse.ScalingGroup(); + scalingGroup.setScalingGroupId("test-ID"); + scalingGroup.setLifecycleState("Active"); + scalingGroups.add(scalingGroup); + response.setScalingGroups(scalingGroups); + return response; + } + } + + private class DescribeScalingInstancesAnswer implements Answer { + @Override + public DescribeScalingInstancesResponse answer(InvocationOnMock invocation) throws Throwable { + DescribeScalingInstancesResponse response = new DescribeScalingInstancesResponse(); + return response; + } + } +} diff --git a/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/EnableAliCloudServerGroupAtomicOperationTest.java b/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/EnableAliCloudServerGroupAtomicOperationTest.java new file mode 100644 index 00000000000..4e87e2fd91f --- /dev/null +++ b/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/EnableAliCloudServerGroupAtomicOperationTest.java @@ -0,0 +1,96 @@ +/* + * 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.clouddriver.alicloud.deploy.ops; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; + +import com.aliyuncs.ess.model.v20140828.DescribeScalingConfigurationsResponse; +import com.aliyuncs.ess.model.v20140828.DescribeScalingConfigurationsResponse.ScalingConfiguration; +import com.aliyuncs.ess.model.v20140828.DescribeScalingGroupsResponse; +import com.aliyuncs.ess.model.v20140828.DescribeScalingGroupsResponse.ScalingGroup; +import com.aliyuncs.ess.model.v20140828.EnableScalingGroupResponse; +import com.aliyuncs.exceptions.ClientException; +import com.netflix.spinnaker.clouddriver.alicloud.deploy.description.EnableAliCloudServerGroupDescription; +import java.util.ArrayList; +import java.util.List; +import org.junit.Before; +import org.junit.Test; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; + +public class EnableAliCloudServerGroupAtomicOperationTest extends CommonAtomicOperation { + + @Before + public void testBefore() throws ClientException { + when(client.getAcsResponse(any())) + .thenAnswer(new DescribeScalingGroupsAnswer()) + .thenAnswer(new DescribeScalingConfigurationsAnswer()) + .thenAnswer(new EnableScalingGroupAnswer()); + } + + @Test + public void testOperate() { + EnableAliCloudServerGroupAtomicOperation operation = + new EnableAliCloudServerGroupAtomicOperation(buildDescription(), clientFactory); + operation.operate(priorOutputs); + } + + private EnableAliCloudServerGroupDescription buildDescription() { + EnableAliCloudServerGroupDescription description = new EnableAliCloudServerGroupDescription(); + description.setRegion(REGION); + description.setCredentials(credentials); + return description; + } + + private class DescribeScalingGroupsAnswer implements Answer { + @Override + public DescribeScalingGroupsResponse answer(InvocationOnMock invocation) throws Throwable { + DescribeScalingGroupsResponse response = new DescribeScalingGroupsResponse(); + List scalingGroups = new ArrayList<>(); + DescribeScalingGroupsResponse.ScalingGroup scalingGroup = + new DescribeScalingGroupsResponse.ScalingGroup(); + scalingGroup.setScalingGroupId("test-ID"); + scalingGroup.setLifecycleState("Inactive"); + scalingGroups.add(scalingGroup); + response.setScalingGroups(scalingGroups); + return response; + } + } + + private class DescribeScalingConfigurationsAnswer + implements Answer { + @Override + public DescribeScalingConfigurationsResponse answer(InvocationOnMock invocation) + throws Throwable { + DescribeScalingConfigurationsResponse response = new DescribeScalingConfigurationsResponse(); + List scalingConfigurations = new ArrayList<>(); + ScalingConfiguration scalingConfiguration = new ScalingConfiguration(); + scalingConfiguration.setScalingConfigurationId("test-ScalingConfigurationId"); + scalingConfigurations.add(scalingConfiguration); + response.setScalingConfigurations(scalingConfigurations); + return response; + } + } + + private class EnableScalingGroupAnswer implements Answer { + @Override + public EnableScalingGroupResponse answer(InvocationOnMock invocation) throws Throwable { + EnableScalingGroupResponse response = new EnableScalingGroupResponse(); + return response; + } + } +} diff --git a/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/ModifyScalingGroupAtomicOperationTest.java b/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/ModifyScalingGroupAtomicOperationTest.java new file mode 100644 index 00000000000..dbe0a8ceae7 --- /dev/null +++ b/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/ModifyScalingGroupAtomicOperationTest.java @@ -0,0 +1,83 @@ +/* + * 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.clouddriver.alicloud.deploy.ops; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; + +import com.aliyuncs.ess.model.v20140828.DescribeScalingGroupsResponse; +import com.aliyuncs.ess.model.v20140828.ModifyScalingGroupResponse; +import com.aliyuncs.exceptions.ClientException; +import com.netflix.spinnaker.clouddriver.alicloud.deploy.description.ModifyScalingGroupDescription; +import com.netflix.spinnaker.clouddriver.alicloud.deploy.description.ModifyScalingGroupDescription.ScalingGroup; +import java.util.ArrayList; +import java.util.List; +import org.junit.Before; +import org.junit.Test; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; + +public class ModifyScalingGroupAtomicOperationTest extends CommonAtomicOperation { + + @Before + public void testBefore() throws ClientException { + when(client.getAcsResponse(any())) + .thenAnswer(new DescribeScalingGroupsAnswer()) + .thenAnswer(new ModifyScalingGroupAnswer()); + } + + @Test + public void testOperate() { + ModifyScalingGroupAtomicOperation operation = + new ModifyScalingGroupAtomicOperation(buildDescription(), objectMapper, clientFactory); + operation.operate(priorOutputs); + } + + private ModifyScalingGroupDescription buildDescription() { + ModifyScalingGroupDescription description = new ModifyScalingGroupDescription(); + description.setRegion(REGION); + description.setCredentials(credentials); + List scalingGroups = new ArrayList<>(); + ScalingGroup scalingGroup = new ScalingGroup(); + scalingGroup.setScalingGroupName("test-Name"); + scalingGroup.setRegion(REGION); + scalingGroups.add(scalingGroup); + description.setScalingGroups(scalingGroups); + return description; + } + + private class DescribeScalingGroupsAnswer implements Answer { + @Override + public DescribeScalingGroupsResponse answer(InvocationOnMock invocation) throws Throwable { + DescribeScalingGroupsResponse response = new DescribeScalingGroupsResponse(); + List scalingGroups = new ArrayList<>(); + DescribeScalingGroupsResponse.ScalingGroup scalingGroup = + new DescribeScalingGroupsResponse.ScalingGroup(); + scalingGroup.setScalingGroupId("test-ID"); + scalingGroups.add(scalingGroup); + response.setScalingGroups(scalingGroups); + return response; + } + } + + private class ModifyScalingGroupAnswer implements Answer { + @Override + public ModifyScalingGroupResponse answer(InvocationOnMock invocation) throws Throwable { + ModifyScalingGroupResponse response = new ModifyScalingGroupResponse(); + return response; + } + } +} diff --git a/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/ResizeAliCloudServerGroupAtomicOperationTest.java b/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/ResizeAliCloudServerGroupAtomicOperationTest.java new file mode 100644 index 00000000000..ac17400869f --- /dev/null +++ b/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/ResizeAliCloudServerGroupAtomicOperationTest.java @@ -0,0 +1,83 @@ +/* + * 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.clouddriver.alicloud.deploy.ops; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; + +import com.aliyuncs.ess.model.v20140828.DescribeScalingGroupsResponse; +import com.aliyuncs.ess.model.v20140828.DescribeScalingGroupsResponse.ScalingGroup; +import com.aliyuncs.ess.model.v20140828.ModifyScalingGroupResponse; +import com.aliyuncs.exceptions.ClientException; +import com.netflix.spinnaker.clouddriver.alicloud.deploy.description.ResizeAliCloudServerGroupDescription; +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import org.junit.Before; +import org.junit.Test; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; + +public class ResizeAliCloudServerGroupAtomicOperationTest extends CommonAtomicOperation { + + @Before + public void testBefore() throws ClientException { + when(client.getAcsResponse(any())) + .thenAnswer(new DescribeScalingGroupsAnswer()) + .thenAnswer(new ModifyScalingGroupAnswer()); + } + + @Test + public void testOperate() { + ResizeAliCloudServerGroupAtomicOperation operation = + new ResizeAliCloudServerGroupAtomicOperation(buildDescription(), clientFactory); + operation.operate(priorOutputs); + } + + private ResizeAliCloudServerGroupDescription buildDescription() { + ResizeAliCloudServerGroupDescription description = new ResizeAliCloudServerGroupDescription(); + description.setRegion(REGION); + description.setCredentials(credentials); + description.setMaxSize(10); + description.setMinSize(1); + LinkedHashMap capacity = new LinkedHashMap<>(); + capacity.put("max", 3); + capacity.put("min", 1); + description.setCapacity(capacity); + return description; + } + + private class DescribeScalingGroupsAnswer implements Answer { + @Override + public DescribeScalingGroupsResponse answer(InvocationOnMock invocation) throws Throwable { + DescribeScalingGroupsResponse response = new DescribeScalingGroupsResponse(); + List scalingGroups = new ArrayList<>(); + ScalingGroup scalingGroup = new ScalingGroup(); + scalingGroup.setScalingGroupId("test-ID"); + scalingGroups.add(scalingGroup); + response.setScalingGroups(scalingGroups); + return response; + } + } + + private class ModifyScalingGroupAnswer implements Answer { + @Override + public ModifyScalingGroupResponse answer(InvocationOnMock invocation) throws Throwable { + ModifyScalingGroupResponse response = new ModifyScalingGroupResponse(); + return response; + } + } +} diff --git a/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/UpdateAliCloudLaunchConfigAtomicOperationTest.java b/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/UpdateAliCloudLaunchConfigAtomicOperationTest.java new file mode 100644 index 00000000000..602c9d2bc93 --- /dev/null +++ b/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/UpdateAliCloudLaunchConfigAtomicOperationTest.java @@ -0,0 +1,81 @@ +/* + * 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.clouddriver.alicloud.deploy.ops; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; + +import com.aliyuncs.ess.model.v20140828.DescribeScalingGroupsResponse; +import com.aliyuncs.ess.model.v20140828.DescribeScalingGroupsResponse.ScalingGroup; +import com.aliyuncs.ess.model.v20140828.ModifyScalingConfigurationResponse; +import com.aliyuncs.exceptions.ClientException; +import com.netflix.spinnaker.clouddriver.alicloud.deploy.description.UpdateAliCloudLaunchConfigDescription; +import java.util.ArrayList; +import java.util.List; +import org.junit.Before; +import org.junit.Test; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; + +public class UpdateAliCloudLaunchConfigAtomicOperationTest extends CommonAtomicOperation { + + @Before + public void testBefore() throws ClientException { + when(client.getAcsResponse(any())) + .thenAnswer(new DescribeScalingGroupsAnswer()) + .thenAnswer(new ModifyScalingConfigurationAnswer()); + } + + @Test + public void testOperate() { + UpdateAliCloudLaunchConfigAtomicOperation operation = + new UpdateAliCloudLaunchConfigAtomicOperation( + buildDescription(), objectMapper, clientFactory); + operation.operate(priorOutputs); + } + + private UpdateAliCloudLaunchConfigDescription buildDescription() { + UpdateAliCloudLaunchConfigDescription description = new UpdateAliCloudLaunchConfigDescription(); + description.setRegion(REGION); + description.setCredentials(credentials); + description.setScalingGroupName("test-Name"); + description.setServerGroupName("test-Name"); + description.setSystemDiskCategory("cloud_ssd"); + return description; + } + + private class ModifyScalingConfigurationAnswer + implements Answer { + @Override + public ModifyScalingConfigurationResponse answer(InvocationOnMock invocation) throws Throwable { + ModifyScalingConfigurationResponse response = new ModifyScalingConfigurationResponse(); + return response; + } + } + + private class DescribeScalingGroupsAnswer implements Answer { + @Override + public DescribeScalingGroupsResponse answer(InvocationOnMock invocation) throws Throwable { + DescribeScalingGroupsResponse response = new DescribeScalingGroupsResponse(); + List scalingGroups = new ArrayList<>(); + ScalingGroup scalingGroup = new ScalingGroup(); + scalingGroup.setScalingGroupId("test-ID"); + scalingGroups.add(scalingGroup); + response.setScalingGroups(scalingGroups); + return response; + } + } +} diff --git a/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/UpsertAliCloudLoadBalancerAtomicOperationTest.java b/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/UpsertAliCloudLoadBalancerAtomicOperationTest.java index e25e136d3ea..da65d4073ca 100644 --- a/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/UpsertAliCloudLoadBalancerAtomicOperationTest.java +++ b/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/UpsertAliCloudLoadBalancerAtomicOperationTest.java @@ -5,7 +5,7 @@ * 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 + * 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, diff --git a/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/UpsertAliCloudSecurityGroupAtomicOperationTest.java b/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/UpsertAliCloudSecurityGroupAtomicOperationTest.java index b90b39a27ed..d60b32c26c6 100644 --- a/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/UpsertAliCloudSecurityGroupAtomicOperationTest.java +++ b/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/deploy/ops/UpsertAliCloudSecurityGroupAtomicOperationTest.java @@ -5,7 +5,7 @@ * 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 + * 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, @@ -18,8 +18,12 @@ import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.when; -import com.aliyuncs.ecs.model.v20140526.*; +import com.aliyuncs.ecs.model.v20140526.AuthorizeSecurityGroupRequest; +import com.aliyuncs.ecs.model.v20140526.AuthorizeSecurityGroupResponse; +import com.aliyuncs.ecs.model.v20140526.CreateSecurityGroupResponse; +import com.aliyuncs.ecs.model.v20140526.DescribeSecurityGroupAttributeResponse; import com.aliyuncs.ecs.model.v20140526.DescribeSecurityGroupAttributeResponse.Permission; +import com.aliyuncs.ecs.model.v20140526.DescribeSecurityGroupsResponse; import com.aliyuncs.ecs.model.v20140526.DescribeSecurityGroupsResponse.SecurityGroup; import com.aliyuncs.exceptions.ClientException; import com.netflix.spinnaker.clouddriver.alicloud.deploy.description.UpsertAliCloudSecurityGroupDescription; diff --git a/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/provider/agent/AliCloudLoadBalancerCachingAgentTest.java b/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/provider/agent/AliCloudLoadBalancerCachingAgentTest.java index a78ce249206..57c0a5b3275 100644 --- a/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/provider/agent/AliCloudLoadBalancerCachingAgentTest.java +++ b/clouddriver-alicloud/src/test/java/com/netflix/spinnaker/clouddriver/alicloud/provider/agent/AliCloudLoadBalancerCachingAgentTest.java @@ -33,6 +33,7 @@ import com.netflix.spinnaker.cats.cache.CacheData; import com.netflix.spinnaker.clouddriver.alicloud.AliCloudProvider; import com.netflix.spinnaker.clouddriver.alicloud.cache.Keys; +import com.netflix.spinnaker.clouddriver.alicloud.model.AliCloudLoadBalancerType; import com.netflix.spinnaker.clouddriver.alicloud.provider.AliProvider; import com.netflix.spinnaker.clouddriver.alicloud.security.AliCloudClientProvider; import com.netflix.spinnaker.clouddriver.alicloud.security.AliCloudCredentialsProvider; @@ -83,7 +84,8 @@ public void testLoadData() { account, client); CacheResult result = agent.loadData(providerCache); - String key = Keys.getLoadBalancerKey(NAME, ACCOUNT, REGION, null); + String key = + Keys.getLoadBalancerKey(NAME, ACCOUNT, REGION, null, AliCloudLoadBalancerType.CLB.ns); List LoadBalancers = (List) result.getCacheResults().get(LOAD_BALANCERS.ns); assertTrue(LoadBalancers.size() == 1); assertTrue(key.equals(LoadBalancers.get(0).getId())); diff --git a/clouddriver-alicloud/src/test/resources/mock/describeLoadBalancerAttributeResponse.json b/clouddriver-alicloud/src/test/resources/mock/describeLoadBalancerAttributeResponse.json index 5d7b558ebf4..1be0bd29017 100644 --- a/clouddriver-alicloud/src/test/resources/mock/describeLoadBalancerAttributeResponse.json +++ b/clouddriver-alicloud/src/test/resources/mock/describeLoadBalancerAttributeResponse.json @@ -53,4 +53,4 @@ "listenerPorts": [ "6443" ] -} \ No newline at end of file +} diff --git a/clouddriver-alicloud/src/test/resources/mock/describeScalingConfigurationsResponse.json b/clouddriver-alicloud/src/test/resources/mock/describeScalingConfigurationsResponse.json index e69de29bb2d..8b137891791 100644 --- a/clouddriver-alicloud/src/test/resources/mock/describeScalingConfigurationsResponse.json +++ b/clouddriver-alicloud/src/test/resources/mock/describeScalingConfigurationsResponse.json @@ -0,0 +1 @@ + diff --git a/clouddriver-alicloud/src/test/resources/mock/listenerAttributes.json b/clouddriver-alicloud/src/test/resources/mock/listenerAttributes.json index f6a323d1094..cefe999634f 100644 --- a/clouddriver-alicloud/src/test/resources/mock/listenerAttributes.json +++ b/clouddriver-alicloud/src/test/resources/mock/listenerAttributes.json @@ -17,4 +17,4 @@ "scheduler": "wrr", "status": "running", "unhealthyThreshold": 3 -} \ No newline at end of file +} diff --git a/clouddriver-alicloud/src/test/resources/mock/loadbalancer.json b/clouddriver-alicloud/src/test/resources/mock/loadbalancer.json index ef628afd940..3bd9ce937f8 100644 --- a/clouddriver-alicloud/src/test/resources/mock/loadbalancer.json +++ b/clouddriver-alicloud/src/test/resources/mock/loadbalancer.json @@ -24,9 +24,3 @@ } ] } - - - - - - diff --git a/clouddriver-alicloud/src/test/resources/mock/scalingGroup.json b/clouddriver-alicloud/src/test/resources/mock/scalingGroup.json index e69de29bb2d..8b137891791 100644 --- a/clouddriver-alicloud/src/test/resources/mock/scalingGroup.json +++ b/clouddriver-alicloud/src/test/resources/mock/scalingGroup.json @@ -0,0 +1 @@ + diff --git a/clouddriver-alicloud/src/test/resources/mock/scalingInstance.json b/clouddriver-alicloud/src/test/resources/mock/scalingInstance.json index e69de29bb2d..8b137891791 100644 --- a/clouddriver-alicloud/src/test/resources/mock/scalingInstance.json +++ b/clouddriver-alicloud/src/test/resources/mock/scalingInstance.json @@ -0,0 +1 @@ + diff --git a/clouddriver-alicloud/src/test/resources/mock/vserverGroup.json b/clouddriver-alicloud/src/test/resources/mock/vserverGroup.json index 0a6d8cd6777..7bc6eced5a4 100644 --- a/clouddriver-alicloud/src/test/resources/mock/vserverGroup.json +++ b/clouddriver-alicloud/src/test/resources/mock/vserverGroup.json @@ -5,4 +5,4 @@ "listeners": [], "rules": [] } -} \ No newline at end of file +} diff --git a/settings.gradle b/settings.gradle index e8540b42fa0..f5a293482a6 100644 --- a/settings.gradle +++ b/settings.gradle @@ -26,7 +26,7 @@ enableFeaturePreview("VERSION_ORDERING_V2") rootProject.name = "clouddriver" def cloudProviderProjects = [ -// 'alicloud' : [':clouddriver-alicloud'], + 'alicloud' : [':clouddriver-alicloud'], 'appengine': [':clouddriver-appengine', ':clouddriver-google-common'], 'aws': [':clouddriver-aws', ':clouddriver-ecs', ':clouddriver-eureka', ':clouddriver-lambda'], // Pull cd-eureka separate "Discover" platform, along with cd-consul? 'aws-minimal': [':clouddriver-aws', ':clouddriver-eureka'],