def delete_security_group()

in lib/muchos/ec2.py [0:0]


    def delete_security_group(self):

        sg_id = None
        ec2 = boto3.client("ec2")
        try:
            response = ec2.describe_security_groups(
                Filters=[
                    {"Name": "group-name", "Values": [self.config.sg_name]}
                ]
            )
            if len(response["SecurityGroups"]) > 0:
                sg_id = response["SecurityGroups"][0]["GroupId"]
        except ClientError:
            pass

        if not sg_id:
            print(
                "Could not find security group '{0}'".format(
                    self.config.sg_name
                )
            )
            return

        print(
            "Attempting to delete security group '{0}' "
            "with id '{1}'...".format(self.config.sg_name, sg_id)
        )
        sg_exists = True
        while sg_exists:
            try:
                request = {"GroupId": sg_id}
                ec2.delete_security_group(**request)
                sg_exists = False
            except ClientError as e:
                print(
                    "Failed to delete security group '{0}' due to "
                    "exception below:\n{1}\nRetrying in 10 sec...".format(
                        self.config.sg_name, e
                    )
                )
                time.sleep(10)
        print("Deleted security group")