Exemplo n.º 1
0
        public async Task CreateFullStack()
        {
            var ec2Client = new AmazonEC2Client(RegionEndpoint.EUWest1);
            var keyResult = await ec2Client.CreateKeyPairAsync(new CreateKeyPairRequest($"comformation-{Guid.NewGuid()}"));

            try
            {
                // Generate template
                var template = CreateFullTemplate();

                var request = new CreateStackRequest
                {
                    StackName    = $"ComformationTestInfraTemplate-{Guid.NewGuid()}",
                    TemplateBody = template.ToString(),
                    Parameters   = new List <Amazon.CloudFormation.Model.Parameter>
                    {
                        new Amazon.CloudFormation.Model.Parameter
                        {
                            ParameterKey   = "KeyPairName",
                            ParameterValue = keyResult.KeyPair.KeyName
                        }
                    }
                };

                try
                {
                    // Create stack
                    var createResponse = await _cloudformation.CreateStackAsync(request);

                    try
                    {
                        // Get created stack details
                        var stack = await _cloudformation.GetCreatedStack(createResponse.StackId);

                        // Validate created stack
                        AssertCreatedStuck(stack, keyResult.KeyPair);
                    }
                    finally
                    {
                        // cleanup
                        var deleteResponse =
                            await _cloudformation.DeleteStackAsync(new DeleteStackRequest
                        {
                            StackName = createResponse.StackId
                        });
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    throw;
                }
            }
            finally
            {
                await ec2Client.DeleteKeyPairAsync(new DeleteKeyPairRequest(keyResult.KeyPair.KeyName));
            }
        }
Exemplo n.º 2
0
        public async Task <string> DeleteStack(string stackName, AsyncCallback callBack)
        {
            var getRequest = new DescribeStacksRequest()
            {
                StackName = stackName
            };
            var request = new DeleteStackRequest()
            {
                StackName = stackName
            };
            var response = await client.DeleteStackAsync(request);

            await Task.Delay(1000);

            callBack(null);
            if (response.HttpStatusCode != System.Net.HttpStatusCode.OK)
            {
                return(response.HttpStatusCode.ToString());
            }
            // client.describestack
            while (true)
            {
                await Task.Delay(5000);

                try
                {
                    var getResponse = await client.DescribeStacksAsync(getRequest);

                    if (getResponse.Stacks == null || getResponse.Stacks.Count == 0)
                    {
                        return("DELETE_COMPLETE");
                    }
                    var stack = getResponse.Stacks[0];
                    if (stack.StackStatus == StackStatus.DELETE_FAILED)
                    {
                        return("DELETE_FAILED");
                    }
                    else if (stack.StackStatus == StackStatus.DELETE_COMPLETE)
                    {
                        return("DELETE_COMPLETE");
                    }
                    else
                    {
                        continue;
                    }
                }
                catch (Amazon.CloudFormation.AmazonCloudFormationException ex)
                {
                    if (ex.Message.Trim() == $"Stack with id {stackName} does not exist".Trim())
                    {
                        return("DELETE_COMPLETE");
                    }
                    throw ex;
                }
            }
        }
Exemplo n.º 3
0
 static void DeleteStack(string stackId)
 {
     Console.WriteLine("Marking {0} for deletion...", stackId);
     cloudFormationClient.DeleteStackAsync(
         new DeleteStackRequest {
         StackName = stackId
     }
         ).Wait();
     WaitForStackDeleted(stackId);
 }
Exemplo n.º 4
0
        public async Task CreateSimpleStack()
        {
            // Generate template
            var template = CreateSimpleTemplate();

            var request = new CreateStackRequest
            {
                StackName    = "ComformationTestSimpleTemplate",
                TemplateBody = template.ToString(),
                Parameters   = new List <Amazon.CloudFormation.Model.Parameter>
                {
                    new Amazon.CloudFormation.Model.Parameter
                    {
                        ParameterKey   = "Param2",
                        ParameterValue = "2"
                    }
                },
            };

            try
            {
                // Create stack
                var createResponse = await _cloudformation.CreateStackAsync(request);

                try
                {
                    // Get created stack details
                    var stack = await GetCreatedStack(createResponse.StackId);

                    // Validate created stack
                    await AssertCreatedStuck(stack, template);
                }
                finally
                {
                    // cleanup
                    var deleteResponse =
                        await _cloudformation.DeleteStackAsync(new DeleteStackRequest
                    {
                        StackName = createResponse.StackId
                    });
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
Exemplo n.º 5
0
        public async Task DeleteService(string name, CPSAuthModel auth)
        {
            try
            {
                AmazonCloudFormationClient cloudFormationClient = new AmazonCloudFormationClient(auth.AccessId, auth.AccessSecret, Amazon.RegionEndpoint.GetBySystemName(auth.AccessRegion));
                var responseCloudFormationClient = await cloudFormationClient.DeleteStackAsync(new DeleteStackRequest()
                {
                    StackName = name.ToLower()
                });

                string bucketName = name.ToLower();

                AmazonS3Client      s3Client = new AmazonS3Client(auth.AccessId, auth.AccessSecret, Amazon.RegionEndpoint.GetBySystemName(auth.AccessRegion));
                ListObjectsResponse response = await s3Client.ListObjectsAsync(new ListObjectsRequest()
                {
                    BucketName = bucketName
                });

                if (response.S3Objects.Count > 0)
                {
                    List <KeyVersion> keys = response.S3Objects.Select(obj => new KeyVersion()
                    {
                        Key = obj.Key
                    }).ToList();
                    DeleteObjectsRequest deleteObjectsRequest = new DeleteObjectsRequest
                    {
                        BucketName = bucketName,
                        Objects    = keys
                    };
                    await s3Client.DeleteObjectsAsync(deleteObjectsRequest);
                }

                //Delete Bucket
                DeleteBucketRequest request = new DeleteBucketRequest
                {
                    BucketName = bucketName
                };

                await s3Client.DeleteBucketAsync(request);
            }
            catch (Exception ex)
            {
                TelemetryClientManager.Instance.TrackException(ex);

                Console.WriteLine(ex.ToString());
            }
        }
Exemplo n.º 6
0
        public async Task <bool> DeleteStack(string stackName, Guid credentialsId)
        {
            Guard.Against.NullOrEmpty(stackName, nameof(stackName));
            Guard.Against.GuidNullOrDefault(credentialsId, nameof(credentialsId));

            var creds = await _credsRepo.GetByIdAsync(credentialsId);

            Guard.Against.Null(creds, nameof(creds));

            CreateClient(creds);
            Guard.Against.Null(_client, nameof(_client));

            var stackRessponse = await _client.DeleteStackAsync(new DeleteStackRequest
            {
                StackName = stackName,
            });

            if (stackRessponse.HttpStatusCode == System.Net.HttpStatusCode.OK)
            {
                return(true);
            }

            throw new Exception("Error deleting stack, HTTPStatusCode: " + stackRessponse.HttpStatusCode);
        }
        public async Task TearDownResourcesAsync()
        {
            using (var cfClient = new AmazonCloudFormationClient())
            {
                var request = new DeleteStackRequest
                {
                    StackName = this.FrontendCloudFormationStack
                };

                try
                {
                    await cfClient.DeleteStackAsync(request);

                    Console.WriteLine($"Frontend CloudFormation Stack {this.FrontendCloudFormationStack} is deleted");
                }
                catch (Exception e)
                {
                    Console.WriteLine($"Frontend CloudFormation Stack {this.FrontendCloudFormationStack} was not deleted: {e.Message}");
                }
            }

            using (var lambdaClient = new AmazonLambdaClient())
            {
                var request = new DeleteFunctionRequest
                {
                    FunctionName = this.DynanmoDBStreamLambdaFunction
                };

                try
                {
                    await lambdaClient.DeleteFunctionAsync(request);

                    Console.WriteLine($"Function {this.DynanmoDBStreamLambdaFunction} is deleted");
                }
                catch (Exception e)
                {
                    Console.WriteLine($"Function {this.DynanmoDBStreamLambdaFunction} was not deleted: {e.Message}");
                }
            }

            using (var cognitoClient = new AmazonCognitoIdentityProviderClient())
            {
                var userPool = (await cognitoClient.ListUserPoolsAsync(new ListUserPoolsRequest {
                    MaxResults = 60
                })).UserPools
                               .FirstOrDefault(x => string.Equals(this.CognitoUserPool, x.Name));

                if (userPool != null)
                {
                    var request = new DeleteUserPoolRequest
                    {
                        UserPoolId = userPool.Id
                    };

                    try
                    {
                        await cognitoClient.DeleteUserPoolAsync(request);

                        Console.WriteLine($"Cognito User Pool {this.CognitoUserPool} is deleted");
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine($"Cognito User Pool {this.CognitoUserPool} was not deleted: {e.Message}");
                    }
                }
            }

            using (var ssmClient = new AmazonSimpleSystemsManagementClient())
            {
                try
                {
                    var parameters = (await ssmClient.GetParametersByPathAsync(new GetParametersByPathRequest
                    {
                        Path = this.ParameterStorePrefix,
                        Recursive = true
                    })).Parameters;

                    Console.WriteLine($"Found {parameters.Count} SSM parameters starting with {this.ParameterStorePrefix}");

                    foreach (var parameter in parameters)
                    {
                        try
                        {
                            await ssmClient.DeleteParameterAsync(new DeleteParameterRequest { Name = parameter.Name });

                            Console.WriteLine($"Parameter {parameter.Name} is deleted");
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine($"Parameter {parameter.Name} was not deleted: {e.Message}");
                        }
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine($"Error deleting SSM Parameters: {e.Message}");
                }
            }

            using (var ddbClient = new AmazonDynamoDBClient())
            {
                var request = new DeleteTableRequest
                {
                    TableName = this.DynamoDBTableName
                };

                try
                {
                    await ddbClient.DeleteTableAsync(request);

                    Console.WriteLine($"Table {this.DynamoDBTableName} is deleted");
                }
                catch (Exception e)
                {
                    Console.WriteLine($"Table {this.DynamoDBTableName} was not deleted: {e.Message}");
                }
            }
        }