Exemple #1
0
        public async Task StopRdsInstance(string instanceIdentifier)
        {
            var instance = await GetRDSInstance();

            var isMultiAZ = instance.MultiAZ;

            if (isMultiAZ)
            {
                ModifyDBInstanceRequest request = new ModifyDBInstanceRequest();
                request.MultiAZ              = false;
                request.ApplyImmediately     = true;
                request.DBInstanceIdentifier = instanceIdentifier;
                try
                {
                    var response = await client.ModifyDBInstanceAsync(request);

                    isMultiAZ = response.DBInstance.MultiAZ;
                    while (isMultiAZ)
                    {
                        await Task.Delay(3000);

                        var newResponse = await GetRDSInstance();

                        isMultiAZ = newResponse.MultiAZ;
                    }
                }
                catch (Exception ex)
                {
                    ;
                }
            }


            var stopRequest = new StopDBInstanceRequest()
            {
                DBInstanceIdentifier = instanceIdentifier
            };
            await client.StopDBInstanceAsync(stopRequest);
        }
        public bool StopDB(string name)
        {
            try
            {
                AWSAuthDetails detail;
                string[]       id = name.Split(',');

                detail = SqlHelper.GetAWSAuth(id[0], "DB");

                //var region = RegionEndpoint.GetBySystemName(detail.Region);
                var region = RegionEndpoint.GetBySystemName(id[id.Length - 1]);
                // Amazon.Runtime.AWSCredentials credentials = new Amazon.Runtime.StoredProfileAWSCredentials(detail.ProfileName);
                AmazonRDSClient        rds         = new AmazonRDSClient(detail.AccessKey, detail.SecretKey, region);
                StopDBInstanceRequest  stopRequest = new StopDBInstanceRequest();
                StopDBInstanceResponse stopResponse;
                string response = "";
                for (var i = 0; i < id.Length - 1; i++)
                {
                    var instance = id[i];
                    stopRequest.DBInstanceIdentifier = instance;
                    stopResponse = rds.StopDBInstance(stopRequest);
                    response     = stopResponse.HttpStatusCode.ToString();
                }

                if (response == "OK")
                {
                    SqlHelper.UpdateDBStatus(id, "stopping");
                    return(true);
                }

                return(false);
            }
            catch (Exception e)
            {
                this.log.Error(e);
                return(false);
            }
        }
Exemple #3
0
        public APIGatewayProxyResponse KeepOff(APIGatewayProxyRequest request, ILambdaContext context)
        {
            // Log entries show up in CloudWatch
            context.Logger.LogLine("Starting KeepOff Function");

            var c               = new AmazonRDSClient();
            var dbs             = new DescribeDBInstancesRequest();
            var tags            = new ListTagsForResourceRequest();
            var RDSResponseList = new List <RDSStatusResponse>();

            try {
                var dbresponse = Task.Run(() => c.DescribeDBInstancesAsync(dbs).Result);
                dbresponse.Result.DBInstances.ForEach(instance =>
                {
                    //log arn on instance
                    //context.Logger.LogLine(instance.DBInstanceArn);
                    var listtagrequest          = new ListTagsForResourceRequest();
                    listtagrequest.ResourceName = instance.DBInstanceArn;
                    //query tags on instance
                    var tagresponse = Task.Run(() => c.ListTagsForResourceAsync(listtagrequest).Result);
                    //iterate through tags
                    tagresponse.Result.TagList.ForEach(tag =>
                    {
                        //check if tag name is keep-off
                        if (tag.Key == "keep-off")
                        {
                            //context.Logger.LogLine(instance.DBInstanceArn);
                            //check if tag value is true
                            if (tag.Value == "true")
                            {
                                //check if instance is on
                                if (instance.DBInstanceStatus == "available")
                                {
                                    //check if instance is on
                                    var RDSstatus             = new RDSStatusResponse();
                                    RDSstatus.RDSInstanceName = instance.DBInstanceIdentifier;
                                    RDSstatus.TagValue        = tag.Value;
                                    RDSstatus.CurrentStatus   = instance.DBInstanceStatus;
                                    RDSstatus.Note            = "Making call to turn off";
                                    RDSResponseList.Add(RDSstatus);


                                    var stopdb = new StopDBInstanceRequest();
                                    stopdb.DBInstanceIdentifier = instance.DBInstanceIdentifier;

                                    var stopresponse = Task.Run(() => c.StopDBInstanceAsync(stopdb));
                                    //Log that db is stopping
                                    context.Logger.LogLine(instance.DBInstanceArn + " has been stopped with status");
                                }
                                else
                                {
                                    var RDSstatus             = new RDSStatusResponse();
                                    RDSstatus.RDSInstanceName = instance.DBInstanceIdentifier;
                                    RDSstatus.TagValue        = tag.Value;
                                    RDSstatus.CurrentStatus   = instance.DBInstanceStatus;
                                    RDSstatus.Note            = "Not in available state";
                                    RDSResponseList.Add(RDSstatus);
                                }
                            }
                            else
                            {
                                var RDSstatus             = new RDSStatusResponse();
                                RDSstatus.RDSInstanceName = instance.DBInstanceIdentifier;
                                RDSstatus.TagValue        = tag.Value;
                                RDSstatus.CurrentStatus   = instance.DBInstanceStatus;
                                RDSstatus.Note            = "Has Tag but Value is not true";
                                RDSResponseList.Add(RDSstatus);
                            }
                        }
                    }

                                                       );
                });


                var response = new APIGatewayProxyResponse
                {
                    StatusCode = (int)HttpStatusCode.OK,
                    Body       = Newtonsoft.Json.JsonConvert.SerializeObject(RDSResponseList),
                    Headers    = new Dictionary <string, string> {
                        { "Content-Type", "application/json" }, { "PracticeWeek", "Rocks!" }
                    }
                };
                return(response);
            } catch (Exception ex) {
                var response = new APIGatewayProxyResponse
                {
                    StatusCode = 500,
                    Body       = Newtonsoft.Json.JsonConvert.SerializeObject(ex),
                    Headers    = new Dictionary <string, string> {
                        { "Content-Type", "application/json" }, { "PracticeWeek", "Rocks!" }
                    }
                };
                return(response);
            }
        }