File Coverage

blib/lib/Akamai/PropertyFetcher.pm
Criterion Covered Total %
statement 29 103 28.1
branch 0 32 0.0
condition 3 25 12.0
subroutine 9 10 90.0
pod 2 2 100.0
total 43 172 25.0


line stmt bran cond sub pod time code
1             package Akamai::PropertyFetcher;
2              
3 1     1   90868 use strict;
  1         2  
  1         44  
4 1     1   6 use warnings;
  1         2  
  1         68  
5 1     1   417 use Akamai::Edgegrid;
  1         123651  
  1         47  
6 1     1   944 use JSON;
  1         12802  
  1         12  
7 1     1   195 use File::Spec;
  1         2  
  1         33  
8 1     1   4 use File::Path 'make_path';
  1         1  
  1         57  
9 1     1   728 use Parallel::ForkManager;
  1         62282  
  1         51  
10 1     1   9 use Carp;
  1         10  
  1         1400  
11              
12             our $VERSION = '0.01';
13              
14             sub new {
15 1     1 1 267884 my ($class, %args) = @_;
16 1         4 my $self = bless {}, $class;
17              
18             $self->{agent} = Akamai::Edgegrid->new(
19             config_file => $args{config_file} || "$ENV{HOME}/.edgerc",
20 1   33     20 section => $args{section} || "default"
      50        
21             );
22 1   50     14 $self->{max_processes} = $args{max_processes} || 4;
23              
24 1         5 return $self;
25             }
26              
27             sub retrieve_property_data {
28 0     0 1   my ($self) = @_;
29 0           my $agent = $self->{agent};
30 0           my $pm = Parallel::ForkManager->new($self->{max_processes});
31              
32             # 基本URL
33 0           my $baseurl = "https://" . $agent->{host};
34              
35             # Endpoint to retrieve contract IDs
36 0           my $contracts_endpoint = "$baseurl/papi/v1/contracts";
37 0           my $contracts_resp = $agent->get($contracts_endpoint);
38 0 0         die "Error retrieving contract ID: " . $contracts_resp->status_line unless $contracts_resp->is_success;
39 0           my $contracts_data = decode_json($contracts_resp->decoded_content);
40 0           my @contract_ids = map { $_->{contractId} } @{ $contracts_data->{contracts}->{items} };
  0            
  0            
41              
42             # Endpoint to retrieve group IDs
43 0           my $groups_endpoint = "$baseurl/papi/v1/groups";
44 0           my $groups_resp = $agent->get($groups_endpoint);
45 0 0         die "Error retrieving group ID: " . $groups_resp->status_line unless $groups_resp->is_success;
46 0           my $groups_data = decode_json($groups_resp->decoded_content);
47 0           my @group_ids = map { $_->{groupId} } @{ $groups_data->{groups}->{items} };
  0            
  0            
48              
49             # Process all combinations of contract IDs and group IDs
50 0           foreach my $contract_id (@contract_ids) {
51 0           foreach my $group_id (@group_ids) {
52 0           my $properties_endpoint = "$baseurl/papi/v1/properties?contractId=$contract_id&groupId=$group_id";
53 0           my $properties_resp = $agent->get($properties_endpoint);
54              
55 0 0 0       if ($properties_resp->is_success) {
    0          
56             # Retrieve property information
57 0           my $properties_data = decode_json($properties_resp->decoded_content);
58 0           my $properties = $properties_data->{properties}->{items};
59              
60 0           foreach my $property (@$properties) {
61             # Fork a new process for each property
62 0 0         $pm->start and next;
63              
64 0           my $property_id = $property->{propertyId};
65 0           my $property_name = $property->{propertyName};
66              
67             # Create a directory for the property
68 0           my $base_dir = "property";
69 0           my $property_dir = File::Spec->catdir($base_dir, $property_name);
70 0 0         make_path($property_dir) unless -d $property_dir;
71              
72             # Retrieve activated version information
73 0           my $activations_endpoint = "$baseurl/papi/v1/properties/$property_id/activations?contractId=$contract_id&groupId=$group_id";
74 0           my $activations_resp = $agent->get($activations_endpoint);
75              
76 0 0         if ($activations_resp->is_success) {
77 0           my $activations_data = decode_json($activations_resp->decoded_content);
78              
79             # Sort activations to get the latest active versions for STAGING and PRODUCTION
80 0           my ($staging_version, $production_version);
81 0           foreach my $activation (sort { $b->{propertyVersion} <=> $a->{propertyVersion} } @{ $activations_data->{activations}->{items} }) {
  0            
  0            
82 0 0 0       if (!defined $staging_version && $activation->{network} eq 'STAGING' && $activation->{status} eq 'ACTIVE') {
      0        
83 0           $staging_version = $activation->{propertyVersion};
84             }
85 0 0 0       if (!defined $production_version && $activation->{network} eq 'PRODUCTION' && $activation->{status} eq 'ACTIVE') {
      0        
86 0           $production_version = $activation->{propertyVersion};
87             }
88             }
89              
90             # If no active version found, skip to the next property
91 0 0 0       unless (defined $staging_version || defined $production_version) {
92 0           warn "No active version found for property ($property_name) - Skipping\n";
93 0           $pm->finish;
94             }
95              
96             # Retrieve and save staging version details if an active version is found
97 0 0         if (defined $staging_version) {
98 0           my $staging_rules_endpoint = "$baseurl/papi/v1/properties/$property_id/versions/$staging_version/rules?contractId=$contract_id&groupId=$group_id";
99 0           my $staging_rules_resp = $agent->get($staging_rules_endpoint);
100              
101 0 0         if ($staging_rules_resp->is_success) {
102 0           my $staging_rules_content = $staging_rules_resp->decoded_content;
103 0           my $staging_file_path = File::Spec->catfile($property_dir, "staging.json");
104              
105 0 0         open my $fh, '>', $staging_file_path or die "Failed to create file: $staging_file_path";
106 0           print $fh $staging_rules_content;
107 0           close $fh;
108              
109 0           print "Saved staging environment active property details: $staging_file_path\n";
110             } else {
111 0           warn "Error retrieving staging version details ($property_name): " . $staging_rules_resp->status_line . " - Skipping\n";
112             }
113             }
114              
115             # Retrieve and save production version details if an active version is found
116 0 0         if (defined $production_version) {
117 0           my $production_rules_endpoint = "$baseurl/papi/v1/properties/$property_id/versions/$production_version/rules?contractId=$contract_id&groupId=$group_id";
118 0           my $production_rules_resp = $agent->get($production_rules_endpoint);
119              
120 0 0         if ($production_rules_resp->is_success) {
121 0           my $production_rules_content = $production_rules_resp->decoded_content;
122 0           my $production_file_path = File::Spec->catfile($property_dir, "production.json");
123              
124 0 0         open my $fh, '>', $production_file_path or die "Failed to create file: $production_file_path";
125 0           print $fh $production_rules_content;
126 0           close $fh;
127              
128 0           print "Saved production environment active property details: $production_file_path\n";
129             } else {
130 0           warn "Error retrieving production version details ($property_name): " . $production_rules_resp->status_line . " - Skipping\n";
131             }
132             }
133             } else {
134 0           warn "Error retrieving activation information ($property_name): " . $activations_resp->status_line . " - Skipping\n";
135             }
136            
137             # End the child process
138 0           $pm->finish;
139             }
140             } elsif ($properties_resp->code == 403 || $properties_resp->code == 404) {
141 0           warn "Error retrieving property list (Contract ID: $contract_id, Group ID: $group_id): " . $properties_resp->status_line . " - Skipping\n";
142             } else {
143 0           die "Unexpected error (Contract ID: $contract_id, Group ID: $group_id): " . $properties_resp->status_line;
144             }
145             }
146             }
147              
148              
149             # 全ての子プロセスの終了を待機
150 0           $pm->wait_all_children;
151             }
152              
153             1;
154              
155             __END__