View Javadoc
1   /*
2    * Copyright (c) 2016 ingenieux Labs
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *
16   */
17  
18  package br.com.ingenieux.mojo.lambda;
19  
20  import java.util.Arrays;
21  import java.util.List;
22  import java.util.regex.Matcher;
23  import java.util.regex.Pattern;
24  
25  public class Arn {
26    private static final List<Pattern> PATTERN_ARNS =
27        Arrays.asList(
28            Pattern.compile(
29                "^arn:" + "(?<partition>[\\w\\-]):" + "(?<service>[\\w\\-]):" + "(?<region>[\\w\\-]):" + "(?<accountId>[\\w\\-]):" + "(?<resource>[\\w\\-])"),
30            Pattern.compile(
31                "^arn:"
32                    + "(?<partition>[\\w\\-]):"
33                    + "(?<service>[\\w\\-]):"
34                    + "(?<region>[\\w\\-]):"
35                    + "(?<accountId>[\\w\\-]):"
36                    + "(?<resourceType>[\\w\\-]):"
37                    + "(?<resource>[\\w\\-])"),
38            Pattern.compile(
39                "^arn:"
40                    + "(?<partition>[\\w\\-]):"
41                    + "(?<service>[\\w\\-]):"
42                    + "(?<region>[\\w\\-]):"
43                    + "(?<accountId>[\\w\\-]):"
44                    + "(?<resourceType>[\\w\\-])/"
45                    + "(?<resource>[\\w\\-])"));
46  
47    String sourceArn;
48  
49    String partition;
50  
51    String service;
52  
53    String region;
54  
55    String accountId;
56  
57    String resource;
58  
59    String resourceType;
60  
61    public Arn(String sourceArn, String partition, String service, String region, String accountId, String resource, String resourceType) {
62      this.sourceArn = sourceArn;
63      this.partition = partition;
64      this.service = service;
65      this.region = region;
66      this.accountId = accountId;
67      this.resource = resource;
68      this.resourceType = resourceType;
69    }
70  
71    public String getSourceArn() {
72      return sourceArn;
73    }
74  
75    public void setSourceArn(String sourceArn) {
76      this.sourceArn = sourceArn;
77    }
78  
79    public String getPartition() {
80      return partition;
81    }
82  
83    public void setPartition(String partition) {
84      this.partition = partition;
85    }
86  
87    public String getService() {
88      return service;
89    }
90  
91    public void setService(String service) {
92      this.service = service;
93    }
94  
95    public String getRegion() {
96      return region;
97    }
98  
99    public void setRegion(String region) {
100     this.region = region;
101   }
102 
103   public String getAccountId() {
104     return accountId;
105   }
106 
107   public void setAccountId(String accountId) {
108     this.accountId = accountId;
109   }
110 
111   public String getResource() {
112     return resource;
113   }
114 
115   public void setResource(String resource) {
116     this.resource = resource;
117   }
118 
119   public String getResourceType() {
120     return resourceType;
121   }
122 
123   public void setResourceType(String resourceType) {
124     this.resourceType = resourceType;
125   }
126 
127   public static Arn lookupArn(String str) {
128     for (Pattern p : PATTERN_ARNS) {
129       Matcher m = p.matcher(str);
130 
131       if (m.matches()) {
132         String partition = m.group("partition");
133         String service = m.group("service");
134         String region = m.group("region");
135         String accountId = m.group("accountId");
136         String resource = m.group("resource");
137         String resourceType = m.group("resourceType");
138 
139         return new Arn(str, partition, service, region, accountId, resource, resourceType);
140       }
141     }
142 
143     return null;
144   }
145 }