Exemplo n.º 1
0
        /// <summary>
        /// Initiates the asynchronous execution of the DescribeStackResources operation.
        /// <seealso cref="Amazon.CloudFormation.IAmazonCloudFormation.DescribeStackResources"/>
        /// </summary>
        ///
        /// <param name="request">Container for the necessary parameters to execute the DescribeStackResources operation.</param>
        /// <param name="cancellationToken">
        ///     A cancellation token that can be used by other objects or threads to receive notice of cancellation.
        /// </param>
        /// <returns>The task object representing the asynchronous operation.</returns>
        public Task <DescribeStackResourcesResponse> DescribeStackResourcesAsync(DescribeStackResourcesRequest request, CancellationToken cancellationToken = default(CancellationToken))
        {
            var marshaller   = new DescribeStackResourcesRequestMarshaller();
            var unmarshaller = DescribeStackResourcesResponseUnmarshaller.GetInstance();

            return(Invoke <IRequest, DescribeStackResourcesRequest, DescribeStackResourcesResponse>(request, marshaller, unmarshaller, signer, cancellationToken));
        }
Exemplo n.º 2
0
        internal DescribeStackResourcesResponse DescribeStackResources(DescribeStackResourcesRequest request)
        {
            var marshaller   = new DescribeStackResourcesRequestMarshaller();
            var unmarshaller = DescribeStackResourcesResponseUnmarshaller.Instance;

            return(Invoke <DescribeStackResourcesRequest, DescribeStackResourcesResponse>(request, marshaller, unmarshaller));
        }
        /// <summary>
        /// <para> Returns AWS resource descriptions for running and deleted stacks. If <c>StackName</c> is specified, all the associated resources that
        /// are part of the stack are returned. If <c>PhysicalResourceId</c> is specified, all the associated resources of the stack the resource
        /// belongs to are returned. </para> <para>For deleted stacks, DescribeStackResources returns resource information for up to 90 days after the
        /// stack has been deleted. </para> <para> You must specify <c>StackName</c> or <c>PhysicalResourceId.</c> In addition, you can specify
        /// <c>LogicalResourceId</c> to filter the returned result. For more information about resources, the <c>LogicalResourceId</c> and
        /// <c>PhysicalResourceId</c> , go to the AWS CloudFormation User Guide. </para> <para><b>NOTE:</b> A ValidationError is returned if you specify
        /// both StackName and PhysicalResourceId in the same request. </para>
        /// </summary>
        ///
        /// <param name="describeStackResourcesRequest">Container for the necessary parameters to execute the DescribeStackResources service method on
        ///           AmazonCloudFormation.</param>
        ///
        /// <returns>The response from the DescribeStackResources service method, as returned by AmazonCloudFormation.</returns>
        ///
        public DescribeStackResourcesResponse DescribeStackResources(DescribeStackResourcesRequest describeStackResourcesRequest)
        {
            IRequest <DescribeStackResourcesRequest> request  = new DescribeStackResourcesRequestMarshaller().Marshall(describeStackResourcesRequest);
            DescribeStackResourcesResponse           response = Invoke <DescribeStackResourcesRequest, DescribeStackResourcesResponse> (request, this.signer, DescribeStackResourcesResponseUnmarshaller.GetInstance());

            return(response);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Initiates the asynchronous execution of the DescribeStackResources operation.
        /// </summary>
        ///
        /// <param name="request">Container for the necessary parameters to execute the DescribeStackResources operation.</param>
        /// <param name="cancellationToken">
        ///     A cancellation token that can be used by other objects or threads to receive notice of cancellation.
        /// </param>
        /// <returns>The task object representing the asynchronous operation.</returns>
        public Task <DescribeStackResourcesResponse> DescribeStackResourcesAsync(DescribeStackResourcesRequest request, System.Threading.CancellationToken cancellationToken = default(CancellationToken))
        {
            var marshaller   = new DescribeStackResourcesRequestMarshaller();
            var unmarshaller = DescribeStackResourcesResponseUnmarshaller.Instance;

            return(InvokeAsync <DescribeStackResourcesRequest, DescribeStackResourcesResponse>(request, marshaller,
                                                                                               unmarshaller, cancellationToken));
        }
Exemplo n.º 5
0
        /// <summary>
        /// <para>Returns AWS resource descriptions for running and deleted stacks. If <c>StackName</c> is specified, all the associated resources that
        /// are part of the stack are returned. If <c>PhysicalResourceId</c> is specified, the associated resources of the stack that the resource
        /// belongs to are returned.</para> <para><b>NOTE:</b>Only the first 100 resources will be returned. If your stack has more resources than this,
        /// you should use ListStackResources instead.</para> <para>For deleted stacks, <c>DescribeStackResources</c> returns resource information for
        /// up to 90 days after the stack has been deleted.</para> <para>You must specify either <c>StackName</c> or <c>PhysicalResourceId</c> , but not
        /// both. In addition, you can specify <c>LogicalResourceId</c> to filter the returned result. For more information about resources, the
        /// <c>LogicalResourceId</c> and <c>PhysicalResourceId</c> , go to the <a href="http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide">AWS CloudFormation User Guide</a> .</para> <para><b>NOTE:</b>A ValidationError is returned if you specify both StackName and
        /// PhysicalResourceId in the same request.</para>
        /// </summary>
        ///
        /// <param name="request">Container for the necessary parameters to execute the DescribeStackResources service method on
        /// AmazonCloudFormation.</param>
        ///
        /// <returns>The response from the DescribeStackResources service method, as returned by AmazonCloudFormation.</returns>
        public DescribeStackResourcesResponse DescribeStackResources(DescribeStackResourcesRequest request)
        {
            var task = DescribeStackResourcesAsync(request);

            try
            {
                return(task.Result);
            }
            catch (AggregateException e)
            {
                ExceptionDispatchInfo.Capture(e.InnerException).Throw();
                return(null);
            }
        }
Exemplo n.º 6
0
        public override void Invoke(AWSCredentials creds, RegionEndpoint region, int maxItems)
        {
            AmazonCloudFormationConfig config = new AmazonCloudFormationConfig();

            config.RegionEndpoint = region;
            ConfigureClient(config);
            AmazonCloudFormationClient client = new AmazonCloudFormationClient(creds, config);

            DescribeStackResourcesResponse resp = new DescribeStackResourcesResponse();
            DescribeStackResourcesRequest  req  = new DescribeStackResourcesRequest
            {
            };

            resp = client.DescribeStackResources(req);
            CheckError(resp.HttpStatusCode, "200");

            foreach (var obj in resp.StackResources)
            {
                AddObject(obj);
            }
        }
Exemplo n.º 7
0
        public IEnumerable <StackResource> GetResources(Stack stack)
        {
            var request = new DescribeStackResourcesRequest {
                StackName = stack.StackName
            };
            DescribeStackResourcesResponse response;

            try
            {
                response = _cloudFormationClient.DescribeStackResources(request);
            }
            catch (AmazonCloudFormationException exception)
            {
                if (exception.Message.Contains(_nonExistenceMarker))
                {
                    return(Enumerable.Empty <StackResource>());
                }
                throw;
            }
            return(response.StackResources);
        }
Exemplo n.º 8
0
        private static async Task <string> GetSQSQueueUrl()
        {
            try
            {
                var cfnRequest = new DescribeStackResourcesRequest()
                {
                    StackName = stackName
                };
                var stack = await cfnClient.DescribeStackResourcesAsync(cfnRequest);

                var sqsQueueUrl = stack.StackResources
                                  .Where(x => x.ResourceType == "AWS::SQS::Queue")
                                  .FirstOrDefault().PhysicalResourceId;

                return(sqsQueueUrl);
            }
            catch (System.Exception ex)
            {
                System.Console.WriteLine(ex.Message);
                throw;
            }
        }
Exemplo n.º 9
0
        public static async Task <AwsSettings> GetAwsSettings(string profileName, string stackName)
        {
            if (string.IsNullOrEmpty(profileName))
            {
                throw new Exception($"Error: No ProfileName provided");
            }

            if (string.IsNullOrEmpty(stackName))
            {
                throw new Exception($"Error: No StackName provided");
            }

            var sharedCredentialsFile = new SharedCredentialsFile(); // AWS finds the shared credentials store for us
            CredentialProfile profile = null;

            if (!sharedCredentialsFile.TryGetProfile(profileName, out profile))
            {
                throw new Exception($"Error: Aws Profile \"{profileName}\" not found in shared credentials store.");
            }

            AWSCredentials creds = null;

            if (!AWSCredentialsFactory.TryGetAWSCredentials(profile, sharedCredentialsFile, out creds))
            {
                throw new Exception($"Error: Could not get AWS Credentials using specified profile \"{profileName}\".");
            }

            var awsSettings = new AwsSettings();

            awsSettings["StackName"] = stackName;

            // Get Original Template
            AmazonCloudFormationClient cfClient = null;
            GetTemplateRequest         getTemplateRequestOriginal = null;

            try
            {
                // Note the need to extract region from the profile!
                cfClient = new AmazonCloudFormationClient(creds, profile.Region);
                getTemplateRequestOriginal = new GetTemplateRequest()
                {
                    StackName     = stackName,
                    TemplateStage = Amazon.CloudFormation.TemplateStage.Original
                };
            }
            catch (Exception ex)
            {
                throw new Exception($"Could not create AmazonCloudFormationClient");
            }

            var templateReponse = cfClient.GetTemplateAsync(getTemplateRequestOriginal).GetAwaiter().GetResult();
            //var templateBodyIndex = templateReponse.StagesAvailable.IndexOf("Original");
            var templateBody = templateReponse.TemplateBody; // Original is in yaml form
            //var tmplYaml = new StringReader(new YamlDotNet.Serialization.SerializerBuilder().Build().Serialize(templateBody));
            var tmplYaml     = new StringReader(templateBody);
            var templYamlObj = new YamlDotNet.Serialization.DeserializerBuilder().Build().Deserialize(tmplYaml);

            templateBody = new YamlDotNet.Serialization.SerializerBuilder().JsonCompatible().Build().Serialize(templYamlObj);
            var jTemplateObjOriginal = JObject.Parse(templateBody);


            // Get Processed Template
            var getTemplateRequestProcessed = new GetTemplateRequest()
            {
                StackName     = stackName,
                TemplateStage = Amazon.CloudFormation.TemplateStage.Processed
            };

            templateReponse = cfClient.GetTemplateAsync(getTemplateRequestProcessed).GetAwaiter().GetResult();
            //var templateBodyIndex = templateReponse.StagesAvailable.IndexOf("Original");
            templateBody = templateReponse.TemplateBody;
            var jTemplateObjProcessed = JObject.Parse(templateBody);

            // Get Stack Resources - note: this call only returns the first 100 resources.
            // We are calling it to get the StackId
            var describeStackResourcesRequest = new DescribeStackResourcesRequest()
            {
                StackName = stackName
            };
            var describeStackResourcesResponse = await cfClient.DescribeStackResourcesAsync(describeStackResourcesRequest);

            if (describeStackResourcesResponse.StackResources.Count == 0)
            {
                throw new Exception($"Error: No resources found for specified stack.");
            }

            // Extract region from StackId ARN -- "arn:aws:cloudformation:us-east-1:..."
            var stackIdParts = describeStackResourcesResponse.StackResources[0].StackId.Split(':');


            var region = stackIdParts[3];

            awsSettings["Region"] = region;

            // Get all stack resources - paginated
            // The the ListStackResourcesResponse does not contain the StackId.
            string nextToken     = null;
            string apiName       = null;
            int    resourceCount = 0;
            var    apiGateways   = new Dictionary <string, AwsSettings.Api>();

            awsSettings["ApiGateways"] = apiGateways;
            do
            {
                var listStackResourcesRequest = new ListStackResourcesRequest()
                {
                    StackName = stackName, NextToken = nextToken
                };
                var listStackResourcesResponse = await cfClient.ListStackResourcesAsync(listStackResourcesRequest);

                nextToken = listStackResourcesResponse.NextToken;

                foreach (var resource in listStackResourcesResponse.StackResourceSummaries)
                {
                    resourceCount++;
                    switch (resource.ResourceType)
                    {
                    case "AWS::Cognito::UserPool":
                    case "AWS::Cognito::IdentityPool":
                    case "AWS::Cognito::UserPoolClient":
                        awsSettings[resource.LogicalResourceId] = resource.PhysicalResourceId;
                        break;

                    case "AWS::ApiGatewayV2::Api":
                        var httpApi = new AwsSettings.Api();
                        apiGateways.Add(resource.LogicalResourceId, httpApi);
                        httpApi.Id   = resource.PhysicalResourceId;
                        httpApi.Type = "HttpApi";
                        apiName      = resource.LogicalResourceId;
                        try
                        {
                            var HttpApiSecureAuthType = (string)jTemplateObjProcessed["Resources"][apiName]["Properties"]["Body"]["components"]["securitySchemes"]["OpenIdAuthorizer"]["type"];
                            if (HttpApiSecureAuthType.Equals("oauth2"))
                            {
                                httpApi.SecurityLevel = AwsSettings.SecurityLevel.JWT;
                            }
                            else
                            {
                                httpApi.SecurityLevel = AwsSettings.SecurityLevel.None;
                            }
                        }
                        catch
                        {
                            httpApi.SecurityLevel = AwsSettings.SecurityLevel.None;
                        }
                        httpApi.Stage = (string)jTemplateObjOriginal["Resources"][apiName]["Properties"]["StageName"];
                        break;

                    case "AWS::ApiGateway::RestApi":
                        var restApi = new AwsSettings.Api();
                        apiGateways.Add(resource.LogicalResourceId, restApi);
                        restApi.Id   = resource.PhysicalResourceId;
                        restApi.Type = "Api";
                        apiName      = resource.LogicalResourceId;
                        try
                        {
                            var apiAuthSecurityType = (string)jTemplateObjProcessed["Resources"][apiName]["Properties"]["Body"]["securityDefinitions"]["AWS_IAM"]["x-amazon-apigateway-authtype"];
                            if (apiAuthSecurityType.Equals("awsSigv4"))
                            {
                                restApi.SecurityLevel = AwsSettings.SecurityLevel.AwsSignatureVersion4;
                            }
                            else
                            {
                                restApi.SecurityLevel = AwsSettings.SecurityLevel.None;
                            }
                        }
                        catch
                        {
                            restApi.SecurityLevel = AwsSettings.SecurityLevel.None;
                        }
                        restApi.Stage = (string)jTemplateObjOriginal["Resources"][apiName]["Properties"]["StageName"];
                        break;
                    }
                }
            } while (nextToken != null);

            if (resourceCount == 0)
            {
                throw new Exception($"Error: No resources found for specified stack.");
            }

            return(awsSettings);
        }
Exemplo n.º 10
0
 /// <summary>
 ///  查询资源栈中资源列表
 /// </summary>
 /// <param name="request">请求参数信息</param>
 /// <returns>请求结果信息</returns>
 public async Task <DescribeStackResourcesResponse> DescribeStackResources(DescribeStackResourcesRequest request)
 {
     return(await new DescribeStackResourcesExecutor().Client(this).Execute <DescribeStackResourcesResponse, DescribeStackResourcesResult, DescribeStackResourcesRequest>(request).ConfigureAwait(false));
 }
Exemplo n.º 11
0
 /// <summary>
 ///  查询资源栈中资源列表
 /// </summary>
 /// <param name="request">请求参数信息</param>
 /// <returns>请求结果信息</returns>
 public DescribeStackResourcesResponse DescribeStackResources(DescribeStackResourcesRequest request)
 {
     return(new DescribeStackResourcesExecutor().Client(this).Execute <DescribeStackResourcesResponse, DescribeStackResourcesResult, DescribeStackResourcesRequest>(request));
 }