Created
March 11, 2019 23:03
-
-
Save joaoferrao/d275a5cd5002fb5f9d4fc0008c87bf4d to your computer and use it in GitHub Desktop.
caws_part_3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// awsEC2 type is created for ARNs belonging to the EC2 service | |
type awsEC2 string | |
// awsECS type is created for ARNs belonging to the ECS service | |
type awsECS string | |
// awsGeneric is a is a generic AWS for services ARNs that don't have | |
// a dedicated type within our application. | |
type awsGeneric string | |
// Generic Resource Handler | |
func (aws *awsGeneric) ConverToResource(shortArn, svc, rgn *string) *SingleResource { | |
return &SingleResource{ARN: shortArn, Region: rgn, Service: svc, ID: shortArn,} | |
} | |
// ConvertToRow converts EC2 shortened ARNs to to a SingleResource type | |
func (aws *awsEC2) ConvertToResource(shortArn, svc, rgn *string) *SingleResource { | |
// ec2 instance/i-23123jj1k1k23jh12 | |
// ec2 security-group/sg-23bn1m231233123m1 | |
// ec2 subnet/subnet-92i3i1i23i1ih1v23 | |
// ec2 vpc/vpc-12i3o1ijkj12jh123 | |
s := strings.Split(*shortArn, "/") | |
return &SingleResource{ARN: shortArn, Region: rgn, Service: svc, Product: &s[0], ID: &s[1],} | |
} | |
// ConvertToRow converts ECS shortened ARNs to to a SingleResource type | |
func (aws *awsECS) ConvertToResource(shortArn, svc, rgn *string) *SingleResource { | |
// ecs cluster/some-ecs-cluster | |
s := strings.Split(*shortArn, "/") | |
return &SingleResource{ARN: shortArn, Region: rgn, Service: svc, Product: &s[0], ID: &s[1],} | |
} | |
// GetResourceRow shortens the ARN and assigns it to the right | |
// service type calling its "ConvertToRow" method. Since we have | |
// a default behaviour funneled towards our awsGeneric type, all | |
// services will be handled. | |
func ConvertArnToSingleResource(arn, svc, rgn *string) *SingleResource { | |
shortArn := ShortArn(arn) | |
switch *svc { | |
case "ec2": | |
res := awsEC2(*svc) | |
return res.ConvertToResource(&shortArn, svc, rgn) | |
case "ecs": | |
res := awsECS(*svc) | |
return res.ConvertToResource(&shortArn, svc, rgn) | |
default: | |
res := awsGeneric(*svc) | |
return res.ConverToResource(&shortArn, svc, rgn) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment