-
Notifications
You must be signed in to change notification settings - Fork 70
/
ip_allowlist.go
99 lines (77 loc) · 2.75 KB
/
ip_allowlist.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package iam
import (
"context"
"errors"
"fmt"
"net/http"
"github.com/akamai/AkamaiOPEN-edgegrid-golang/v9/pkg/session"
)
type (
// GetIPAllowlistStatusResponse contains response from the GetIPAllowlistStatus endpoint.
GetIPAllowlistStatusResponse struct {
Enabled bool `json:"enabled"`
}
)
var (
// ErrDisableIPAllowlist is returned when DisableIPAllowlist fails.
ErrDisableIPAllowlist = errors.New("disable ip allowlist")
// ErrEnableIPAllowlist is returned when EnableIPAllowlist fails.
ErrEnableIPAllowlist = errors.New("enable ip allowlist")
// ErrGetIPAllowlistStatus is returned when GetIPAllowlistStatus fails.
ErrGetIPAllowlistStatus = errors.New("get ip allowlist status")
)
func (i *iam) DisableIPAllowlist(ctx context.Context) error {
logger := i.Log(ctx)
logger.Debug("DisableIPAllowlist")
uri := "/identity-management/v3/user-admin/ip-acl/allowlist/disable"
req, err := http.NewRequestWithContext(ctx, http.MethodPost, uri, nil)
if err != nil {
return fmt.Errorf("%w: failed to create request: %s", ErrDisableIPAllowlist, err)
}
resp, err := i.Exec(req, nil, nil)
if err != nil {
return fmt.Errorf("%w: request failed: %s", ErrDisableIPAllowlist, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusNoContent {
return fmt.Errorf("%s: %w", ErrDisableIPAllowlist, i.Error(resp))
}
return nil
}
func (i *iam) EnableIPAllowlist(ctx context.Context) error {
logger := i.Log(ctx)
logger.Debug("EnableIPAllowlist")
uri := "/identity-management/v3/user-admin/ip-acl/allowlist/enable"
req, err := http.NewRequestWithContext(ctx, http.MethodPost, uri, nil)
if err != nil {
return fmt.Errorf("%w: failed to create request: %s", ErrEnableIPAllowlist, err)
}
resp, err := i.Exec(req, nil, nil)
if err != nil {
return fmt.Errorf("%w: request failed: %s", ErrEnableIPAllowlist, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusNoContent {
return fmt.Errorf("%s: %w", ErrEnableIPAllowlist, i.Error(resp))
}
return nil
}
func (i *iam) GetIPAllowlistStatus(ctx context.Context) (*GetIPAllowlistStatusResponse, error) {
logger := i.Log(ctx)
logger.Debug("GetIPAllowlistStatus")
uri := "/identity-management/v3/user-admin/ip-acl/allowlist/status"
req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, fmt.Errorf("%w: failed to create request: %s", ErrGetIPAllowlistStatus, err)
}
var result GetIPAllowlistStatusResponse
resp, err := i.Exec(req, &result)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrGetIPAllowlistStatus, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s: %w", ErrGetIPAllowlistStatus, i.Error(resp))
}
return &result, nil
}