| 1 | package AWS::ARN; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use Moo; | ||
| 6 | use Types::Standard qw/Str/; | ||
| 7 | use Type::Utils; | ||
| 8 | |||
| 9 | our $VERSION = '0.003'; | ||
| 10 | |||
| 11 | use overload '""' => sub { | ||
| 12 | shift->arn; | ||
| 13 | }; | ||
| 14 | |||
| 15 | my $partitionRe = my $serviceRe = qr{[\w-]+}; | ||
| 16 | my $regionRe = qr{[\w-]*}; | ||
| 17 | my $account_idRe = qr{\d*}; | ||
| 18 | my $resource_idRe = qr{.+}; | ||
| 19 | my $arnRe = qr{arn:$partitionRe:$serviceRe:$regionRe:$account_idRe:$resource_idRe}; | ||
| 20 | my $Arn = declare( | ||
| 21 | as Str, | ||
| 22 | where { m{^$arnRe$} }, | ||
| 23 | message { "$_ is not a valid ARN" } | ||
| 24 | ); | ||
| 25 | my $ArnRegion = declare( | ||
| 26 | as Str, | ||
| 27 | where { m{^$regionRe$} }, | ||
| 28 | message { "$_ is not a valid AWS Region" } | ||
| 29 | ); | ||
| 30 | my $ArnPartition = declare( | ||
| 31 | as Str, | ||
| 32 | where { m{^$partitionRe$} }, | ||
| 33 | message { "$_ is not a valid AWS Partitition" } | ||
| 34 | ); | ||
| 35 | my $ArnService = declare( | ||
| 36 | as Str, | ||
| 37 | where { m{^$serviceRe$} }, | ||
| 38 | message { "$_ is not a valid AWS Service" } | ||
| 39 | ); | ||
| 40 | my $ArnAccountID = declare( | ||
| 41 | as Str, | ||
| 42 | where => { m{^$account_idRe$} }, | ||
| 43 | message { "$_ is not a valid AWS Account ID" } | ||
| 44 | ); | ||
| 45 | my $ArnResourceID = declare( | ||
| 46 | as Str, | ||
| 47 | where { m{^$resource_idRe$} }, | ||
| 48 | message { "$_ is not a valid AWS Resource" }, | ||
| 49 | ); | ||
| 50 | |||
| 51 | sub _split_arn { | ||
| 52 | my $self = shift; | ||
| 53 | my ($index) = @_; | ||
| 54 | return "" unless $self->_has_arn; | ||
| 55 | my @parts = split( /:/, $self->arn, 6 ); | ||
| 56 | return $parts[$index||0]||""; | ||
| 57 | } | ||
| 58 | |||
| 59 | has arn => ( | ||
| 60 | is => 'rw', | ||
| 61 | isa => $Arn, | ||
| 62 | lazy => 1, | ||
| 63 | builder => '_build_arn', | ||
| 64 | clearer => '_clear_arn', | ||
| 65 | predicate => '_has_arn', | ||
| 66 | trigger => 1, | ||
| 67 | ); | ||
| 68 | |||
| 69 | has partition => ( | ||
| 70 | is => 'rw', | ||
| 71 | isa => $ArnPartition, | ||
| 72 | lazy => 1, | ||
| 73 | builder => '_build_partition', | ||
| 74 | clearer => '_clear_partition', | ||
| 75 | default => 'aws', | ||
| 76 | trigger => sub { shift->_clear_arn }, | ||
| 77 | ); | ||
| 78 | |||
| 79 | has service => ( | ||
| 80 | is => 'rw', | ||
| 81 | isa => $ArnService, | ||
| 82 | lazy => 1, | ||
| 83 | required => 1, | ||
| 84 | builder => '_build_service', | ||
| 85 | clearer => '_clear_service', | ||
| 86 | trigger => sub { shift->_clear_arn }, | ||
| 87 | ); | ||
| 88 | |||
| 89 | has region => ( | ||
| 90 | is => 'rw', | ||
| 91 | isa => $ArnRegion, | ||
| 92 | lazy => 1, | ||
| 93 | builder => '_build_region', | ||
| 94 | clearer => '_clear_region', | ||
| 95 | trigger => sub { shift->_clear_arn }, | ||
| 96 | ); | ||
| 97 | |||
| 98 | has account_id => ( | ||
| 99 | is => 'rw', | ||
| 100 | isa => $ArnAccountID, | ||
| 101 | lazy => 1, | ||
| 102 | builder => '_build_account_id', | ||
| 103 | clearer => '_clear_account_id', | ||
| 104 | trigger => sub { shift->_clear_arn }, | ||
| 105 | ); | ||
| 106 | |||
| 107 | has resource_id => ( | ||
| 108 | is => 'rw', | ||
| 109 | isa => $ArnResourceID, | ||
| 110 | lazy => 1, | ||
| 111 | builder => '_build_resource_id', | ||
| 112 | clearer => '_clear_resource_id', | ||
| 113 | trigger => sub { shift->_clear_arn }, | ||
| 114 | ); | ||
| 115 | |||
| 116 | sub _build_arn { | ||
| 117 | my $self = shift; | ||
| 118 | my $arn = join( ':', | ||
| 119 | 'arn', | ||
| 120 | $self->partition, | ||
| 121 | $self->service, | ||
| 122 | $self->region, | ||
| 123 | $self->account_id, | ||
| 124 | $self->resource_id, | ||
| 125 | ); | ||
| 126 | } | ||
| 127 | |||
| 128 | sub _build_partition { | ||
| 129 | shift->_split_arn( 1 ) | ||
| 130 | } | ||
| 131 | sub _build_service { | ||
| 132 | shift->_split_arn( 2 ) | ||
| 133 | } | ||
| 134 | sub _build_region { | ||
| 135 | shift->_split_arn( 3 ) | ||
| 136 | } | ||
| 137 | sub _build_account_id { | ||
| 138 | shift->_split_arn( 4 ) | ||
| 139 | } | ||
| 140 | sub _build_resource_id { | ||
| 141 | shift->_split_arn( 5 ) | ||
| 142 | } | ||
| 143 | sub _trigger_arn { | ||
| 144 | my $self = shift; | ||
| 145 | $self->_clear_partition; | ||
| 146 | $self->_clear_service; | ||
| 147 | $self->_clear_region; | ||
| 148 | $self->_clear_account_id; | ||
| 149 | $self->_clear_resource_id; | ||
| 150 | } | ||
| 151 | |||
| 152 | around BUILDARGS => sub { | ||
| 153 | my ( $orig, $class, @args ) = @_; | ||
| 154 | |||
| 155 | return { arn => $args[0] } | ||
| 156 | if @args == 1 && !ref $args[0]; | ||
| 157 | |||
| 158 | return $class->$orig( @args ); | ||
| 159 | }; | ||
| 160 | |||
| 161 | no Try::Tiny; | ||
| 162 | no Type::Utils; | ||
| 163 | no Types::Standard; | ||
| 164 | no Moo; | ||
| 165 | |||
| 166 | 1; | ||
| 167 | |||
| 168 | __END__ |