github.com/akamai/AkamaiOPEN-edgegrid-golang/v2@v2.17.0/pkg/appsec/ip_geo_protection.go (about) 1 package appsec 2 3 import ( 4 "context" 5 "fmt" 6 "net/http" 7 8 validation "github.com/go-ozzo/ozzo-validation/v4" 9 ) 10 11 type ( 12 // The IPGeoProtection interface supports retrieving and updating IPGeo protection for a configuration and policy. 13 // 14 // https://developer.akamai.com/api/cloud_security/application_security/v1.html#protections 15 IPGeoProtection interface { 16 // GetIPGeoProtections retrieves the current IPGeo protection protection setting for a configuration and policy. 17 // 18 // https://developer.akamai.com/api/cloud_security/application_security/v1.html#getprotections 19 // Deprecated: this method will be removed in a future release. Use GetIPGeoProtection instead. 20 GetIPGeoProtections(ctx context.Context, params GetIPGeoProtectionsRequest) (*GetIPGeoProtectionsResponse, error) 21 22 // GetIPGeoProtection retrieves the current IPGeo protection protection setting for a configuration and policy. 23 // 24 // https://developer.akamai.com/api/cloud_security/application_security/v1.html#getprotections 25 GetIPGeoProtection(ctx context.Context, params GetIPGeoProtectionRequest) (*GetIPGeoProtectionResponse, error) 26 27 // SetIPGeoProtection updates the IPGeo protection protection setting for a configuration and policy. 28 // 29 // https://developer.akamai.com/api/cloud_security/application_security/v1.html#putprotections 30 UpdateIPGeoProtection(ctx context.Context, params UpdateIPGeoProtectionRequest) (*UpdateIPGeoProtectionResponse, error) 31 } 32 33 // GetIPGeoProtectionRequest is used to retrieve the IPGeo protection settings. 34 GetIPGeoProtectionRequest struct { 35 ConfigID int `json:"-"` 36 Version int `json:"-"` 37 PolicyID string `json:"-"` 38 ApplyApplicationLayerControls bool `json:"applyNetworkLayerControls"` 39 } 40 41 // GetIPGeoProtectionResponse is returned from a call to GetIPGeoProtection. 42 GetIPGeoProtectionResponse ProtectionsResponse 43 44 // GetIPGeoProtectionsRequest is used to retrieve the IPGeo protection settings. 45 // Deprecated: this struct will be removed in a future release. 46 GetIPGeoProtectionsRequest struct { 47 ConfigID int `json:"-"` 48 Version int `json:"-"` 49 PolicyID string `json:"-"` 50 ApplyNetworkLayerControls bool `json:"applyNetworkLayerControls"` 51 } 52 53 // GetIPGeoProtectionsResponse is returned from a call to GetIPGeoProtections. 54 // Deprecated: this struct will be removed in a future release. 55 GetIPGeoProtectionsResponse ProtectionsResponse 56 57 // UpdateIPGeoProtectionRequest is used to modify the IPGeo protection settings. 58 UpdateIPGeoProtectionRequest struct { 59 ConfigID int `json:"-"` 60 Version int `json:"-"` 61 PolicyID string `json:"-"` 62 ApplyNetworkLayerControls bool `json:"applyNetworkLayerControls"` 63 } 64 65 // UpdateIPGeoProtectionResponse is returned from a call to UpdateIPGeoProtection. 66 UpdateIPGeoProtectionResponse ProtectionsResponse 67 ) 68 69 // Validate validates a GetIPGeoProtectionRequest. 70 func (v GetIPGeoProtectionRequest) Validate() error { 71 return validation.Errors{ 72 "ConfigID": validation.Validate(v.ConfigID, validation.Required), 73 "Version": validation.Validate(v.Version, validation.Required), 74 "PolicyID": validation.Validate(v.PolicyID, validation.Required), 75 }.Filter() 76 } 77 78 // Validate validates a GetIPGeoProtectionsRequest. 79 func (v GetIPGeoProtectionsRequest) Validate() error { 80 return validation.Errors{ 81 "ConfigID": validation.Validate(v.ConfigID, validation.Required), 82 "Version": validation.Validate(v.Version, validation.Required), 83 "PolicyID": validation.Validate(v.PolicyID, validation.Required), 84 }.Filter() 85 } 86 87 // Validate validates an UpdateIPGeoProtectionRequest. 88 func (v UpdateIPGeoProtectionRequest) Validate() error { 89 return validation.Errors{ 90 "ConfigID": validation.Validate(v.ConfigID, validation.Required), 91 "Version": validation.Validate(v.Version, validation.Required), 92 "PolicyID": validation.Validate(v.PolicyID, validation.Required), 93 }.Filter() 94 } 95 96 func (p *appsec) GetIPGeoProtection(ctx context.Context, params GetIPGeoProtectionRequest) (*GetIPGeoProtectionResponse, error) { 97 logger := p.Log(ctx) 98 logger.Debug("GetIPGeoProtection") 99 100 if err := params.Validate(); err != nil { 101 return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error()) 102 } 103 104 uri := fmt.Sprintf( 105 "/appsec/v1/configs/%d/versions/%d/security-policies/%s/protections", 106 params.ConfigID, 107 params.Version, 108 params.PolicyID) 109 110 req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil) 111 if err != nil { 112 return nil, fmt.Errorf("failed to create GetIPGeoProtection request: %w", err) 113 } 114 115 var result GetIPGeoProtectionResponse 116 resp, err := p.Exec(req, &result) 117 if err != nil { 118 return nil, fmt.Errorf("get IPGeo protection request failed: %w", err) 119 } 120 if resp.StatusCode != http.StatusOK { 121 return nil, p.Error(resp) 122 } 123 124 return &result, nil 125 } 126 127 func (p *appsec) GetIPGeoProtections(ctx context.Context, params GetIPGeoProtectionsRequest) (*GetIPGeoProtectionsResponse, error) { 128 logger := p.Log(ctx) 129 logger.Debug("GetIPGeoProtections") 130 131 if err := params.Validate(); err != nil { 132 return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error()) 133 } 134 135 uri := fmt.Sprintf( 136 "/appsec/v1/configs/%d/versions/%d/security-policies/%s/protections", 137 params.ConfigID, 138 params.Version, 139 params.PolicyID) 140 141 req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil) 142 if err != nil { 143 return nil, fmt.Errorf("failed to create GetIPGeoProtections request: %w", err) 144 } 145 146 var result GetIPGeoProtectionsResponse 147 resp, err := p.Exec(req, &result) 148 if err != nil { 149 return nil, fmt.Errorf("get IPGeo protections request failed: %w", err) 150 } 151 if resp.StatusCode != http.StatusOK { 152 return nil, p.Error(resp) 153 } 154 155 return &result, nil 156 } 157 158 func (p *appsec) UpdateIPGeoProtection(ctx context.Context, params UpdateIPGeoProtectionRequest) (*UpdateIPGeoProtectionResponse, error) { 159 logger := p.Log(ctx) 160 logger.Debug("UpdateIPGeoProtection") 161 162 if err := params.Validate(); err != nil { 163 return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error()) 164 } 165 166 uri := fmt.Sprintf( 167 "/appsec/v1/configs/%d/versions/%d/security-policies/%s/protections", 168 params.ConfigID, 169 params.Version, 170 params.PolicyID, 171 ) 172 173 req, err := http.NewRequestWithContext(ctx, http.MethodPut, uri, nil) 174 if err != nil { 175 return nil, fmt.Errorf("failed to create UpdateIPGeoProtection request: %w", err) 176 } 177 178 var result UpdateIPGeoProtectionResponse 179 resp, err := p.Exec(req, &result, params) 180 if err != nil { 181 return nil, fmt.Errorf("update IPGeo protection request failed: %w", err) 182 } 183 if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated { 184 return nil, p.Error(resp) 185 } 186 187 return &result, nil 188 }