def test_s3_url_maker(self, m_get_s3_domain): m_s3 = mock.Mock() m_s3.get_bucket_location.return_value = {"LocationConstraint": None} actual = s3_url_maker("test-bucket", "test-key/1", m_s3) self.assertEqual("https://test-bucket.s3.amazonaws.com/test-key/1", actual) m_s3.get_bucket_location.return_value = {"LocationConstraint": "us-west-2"} actual = s3_url_maker("test-bucket", "test-key/1", m_s3) self.assertEqual( "https://test-bucket.s3-us-west-2.amazonaws.com/test-key/1", actual ) m_get_s3_domain.assert_called_once()
def create( cls, region: AWSRegionObject, stack_name: str, template: Template, parameters: List[Parameter] = None, tags: List[Tag] = None, disable_rollback: bool = True, test_name: str = "", uuid: UUID = None, ) -> "Stack": uuid = uuid if uuid else uuid4() cfn_client = region.client("cloudformation") parameters = [p.dump() for p in parameters] if parameters else [] tags = [t.dump() for t in tags] if tags else [] if isinstance(region.s3bucket, (S3Bucket, mock.Mock)): bucket_name: str = region.s3bucket.name else: raise TypeError("region object has unset bucket object") template.url = s3_url_maker(bucket_name, template.s3_key, region.client("s3")) stack_id = cfn_client.create_stack( StackName=stack_name, TemplateURL=template.url, Parameters=parameters, DisableRollback=disable_rollback, Tags=tags, Capabilities=Capabilities.ALL, )["StackId"] stack = cls(region, stack_id, template, test_name, uuid) # fetch property values from cfn stack.refresh() return stack
def create( cls, region: TestRegion, stack_name: str, template: Template, tags: List[Tag] = None, disable_rollback: bool = True, test_name: str = "", uuid: UUID = None, ) -> "Stack": parameters = cls._cfn_format_parameters(region.parameters) uuid = uuid if uuid else uuid4() cfn_client = region.client("cloudformation") tags = [t.dump() for t in tags] if tags else [] bucket_name: str = region.s3_bucket.name template.url = s3_url_maker(bucket_name, template.s3_key, region.client("s3")) stack_id = cfn_client.create_stack( StackName=stack_name, TemplateURL=template.url, Parameters=parameters, DisableRollback=disable_rollback, Tags=tags, Capabilities=Capabilities.ALL, )["StackId"] stack = cls(region, stack_id, template, test_name, uuid) # fetch property values from cfn stack.refresh() return stack
def _upload(self, bucket_name: str, prefix: str = "") -> str: s3_client = self.client_factory_instance.get("s3") s3_client.upload_file(str(self.template_path), bucket_name, prefix + self.template_path.name) return s3_url_maker( bucket_name, f"{prefix}{self.template_path.name}", self.client_factory_instance, )
def create( cls, region: TestRegion, stack_name: str, template: Template, tags: List[Tag] = None, disable_rollback: bool = True, test_name: str = "", uuid: UUID = None, ) -> "Stack": parameters = cls._cfn_format_parameters(region.parameters) uuid = uuid if uuid else uuid4() cfn_client = region.client("cloudformation") tags = [t.dump() for t in tags] if tags else [] template = Template( template_path=template.template_path, project_root=template.project_root, s3_key_prefix=template.s3_key_prefix, url=s3_url_maker( region.s3_bucket.name, template.s3_key, region.client("s3"), region.s3_bucket.auto_generated, ), template_cache=tcat_template_cache, ) create_options = { "StackName": stack_name, "TemplateURL": template.url, "Parameters": parameters, "DisableRollback": disable_rollback, "Tags": tags, "Capabilities": Capabilities.ALL, } if region.role_arn: create_options["RoleARN"] = region.role_arn stack_id = cfn_client.create_stack(**create_options)["StackId"] stack = cls(region, stack_id, template, test_name, uuid) # fetch property values from cfn stack.refresh() return stack