From bb420fe58a5d095e20cbf2383c84ff514a7b738e Mon Sep 17 00:00:00 2001 From: Sai Dadireddy Date: Thu, 19 Sep 2024 09:02:12 -0700 Subject: [PATCH 01/12] Add CRUD endpoints for infrastructure access endpoints --- .changelog/3183.txt | 3 + infrastructure_targets.go | 236 +++++++++++++++++++++++++++ infrastructure_targets_test.go | 287 +++++++++++++++++++++++++++++++++ 3 files changed, 526 insertions(+) create mode 100644 .changelog/3183.txt create mode 100644 infrastructure_targets.go create mode 100644 infrastructure_targets_test.go diff --git a/.changelog/3183.txt b/.changelog/3183.txt new file mode 100644 index 00000000000..69e4ec2d5eb --- /dev/null +++ b/.changelog/3183.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +infrastructure_targets: initialize CRUD endpoints for infrastructure access endpoints +``` \ No newline at end of file diff --git a/infrastructure_targets.go b/infrastructure_targets.go new file mode 100644 index 00000000000..7c6e5cb658a --- /dev/null +++ b/infrastructure_targets.go @@ -0,0 +1,236 @@ +package cloudflare + +import ( + "context" + "errors" + "fmt" + "net/http" + "time" + + "github.com/goccy/go-json" +) + +var ErrMissingTargetId = errors.New("required target id missing") + +// AccessApplication represents an Access application. +type Target struct { + Hostname string `json:"hostname"` + ID string `json:"id"` + IP IPInfo `json:"ip"` + CreatedAt time.Time `json:"created_at"` + ModifiedAt time.Time `json:"modified_at"` +} + +type IPDetails struct { + IpAddr string `json:"ip_addr"` + VirtualNetworkId string `json:"virtual_network_id"` +} + +type IPInfo struct { + IPV4 *IPDetails `json:"ipv4,omitempty"` + IPV6 *IPDetails `json:"ipv6,omitempty"` +} + +type InfrastructureTargetParams struct { + Hostname string `json:"hostname"` + IP IPInfo `json:"ip"` +} + +type CreateInfrastructureTargetParams struct { + InfrastructureTargetParams +} + +type UpdateInfrastructureTargetParams struct { + ID string `json:"hostname"` + ModifyParams InfrastructureTargetParams `json:"modify_params"` +} + +// TargetDetailResponse is the API response, containing a single access target. +type TargetDetailResponse struct { + Result Target `json:"result"` + Response +} + +type TargetListDetailResponse struct { + Result []Target `json:"result"` + Response + ResultInfo `json:"result_info"` +} + +type TargetListParams struct { + CreatedAfter string `url:"created_after,omitempty"` + Hostname string `url:"hostname,omitempty"` + HostnameContains string `url:"hostname_contains,omitempty"` + IPV4 string `url:"ip_v4,omitempty"` + IPV6 string `url:"ip_v6,omitempty"` + ModifedAfter string `url:"modified_after,omitempty"` + VirtualNetworkId string `url:"virtual_network_id,omitempty"` + + ResultInfo +} + +// ListInfrastructureTargets returns all applications within an account or zone. +// +// Account API reference: https://developers.cloudflare.com/api/operations/infra-targets-list +func (api *API) ListInfrastructureTargets(ctx context.Context, rc *ResourceContainer, params TargetListParams) ([]Target, *ResultInfo, error) { + if rc.Identifier == "" { + return []Target{}, &ResultInfo{}, ErrMissingAccountID + } + + baseURL := fmt.Sprintf("/%s/%s/infrastructure/targets", rc.Level, rc.Identifier) + + autoPaginate := true + if params.PerPage >= 1 || params.Page >= 1 { + autoPaginate = false + } + + if params.PerPage < 1 { + params.PerPage = 25 + } + + if params.Page < 1 { + params.Page = 1 + } + + var applications []Target + var r TargetListDetailResponse + + for { + uri := buildURI(baseURL, params) + + res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil) + if err != nil { + return []Target{}, &ResultInfo{}, fmt.Errorf("%s: %w", errMakeRequestError, err) + } + + err = json.Unmarshal(res, &r) + if err != nil { + return []Target{}, &ResultInfo{}, fmt.Errorf("%s: %w", errUnmarshalError, err) + } + applications = append(applications, r.Result...) + params.ResultInfo = r.ResultInfo.Next() + if params.ResultInfo.Done() || !autoPaginate { + break + } + } + + return applications, &r.ResultInfo, nil +} + +// CreateInfrastructureTarget creates a new infrastructure target. +// +// Account API reference: https://developers.cloudflare.com/api/operations/infra-targets-post +func (api *API) CreateInfrastructureTarget(ctx context.Context, rc *ResourceContainer, params CreateInfrastructureTargetParams) (Target, error) { + if rc.Identifier == "" { + return Target{}, ErrMissingAccountID + } + + uri := fmt.Sprintf("/%s/%s/infrastructure/targets", rc.Level, rc.Identifier) + + res, err := api.makeRequestContext(ctx, http.MethodPost, uri, params) + if err != nil { + return Target{}, fmt.Errorf("%s: %w", errMakeRequestError, err) + } + + var targetDetailResponse TargetDetailResponse + err = json.Unmarshal(res, &targetDetailResponse) + if err != nil { + return Target{}, fmt.Errorf("%s: %w", errUnmarshalError, err) + } + + return targetDetailResponse.Result, nil +} + +// UpdateInfrastructureTarget updates an existing infrastructure target. +// +// Account API reference: https://developers.cloudflare.com/api/operations/infra-targets-put +func (api *API) UpdateInfrastructureTarget(ctx context.Context, rc *ResourceContainer, params UpdateInfrastructureTargetParams) (Target, error) { + if rc.Identifier == "" { + return Target{}, ErrMissingAccountID + } + + if params.ID == "" { + return Target{}, ErrMissingTargetId + } + + uri := fmt.Sprintf( + "/%s/%s/infrastructure/targets/%s", + rc.Level, + rc.Identifier, + params.ID, + ) + + res, err := api.makeRequestContext(ctx, http.MethodPut, uri, params.ModifyParams) + if err != nil { + return Target{}, fmt.Errorf("%s: %w", errMakeRequestError, err) + } + + var targetDetailResponse TargetDetailResponse + err = json.Unmarshal(res, &targetDetailResponse) + if err != nil { + return Target{}, fmt.Errorf("%s: %w", errUnmarshalError, err) + } + + return targetDetailResponse.Result, nil +} + +// GetInfrastructureTarget returns a single infrastructure target based on target ID +// ID for either account or zone. +// +// Account API reference: https://developers.cloudflare.com/api/operations/infra-targets-get +func (api *API) GetInfrastructureTarget(ctx context.Context, rc *ResourceContainer, targetID string) (Target, error) { + if rc.Identifier == "" { + return Target{}, ErrMissingAccountID + } + + if targetID == "" { + return Target{}, ErrMissingTargetId + } + + uri := fmt.Sprintf( + "/%s/%s/infrastructure/targets/%s", + rc.Level, + rc.Identifier, + targetID, + ) + + res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil) + if err != nil { + return Target{}, fmt.Errorf("%s: %w", errMakeRequestError, err) + } + + var targetDetailResponse TargetDetailResponse + err = json.Unmarshal(res, &targetDetailResponse) + if err != nil { + return Target{}, fmt.Errorf("%s: %w", errUnmarshalError, err) + } + + return targetDetailResponse.Result, nil +} + +// DeleteInfrastructureTarget deletes an infrastructure target. +// +// Account API reference: https://developers.cloudflare.com/api/operations/infra-targets-delete +func (api *API) DeleteInfrastructureTarget(ctx context.Context, rc *ResourceContainer, targetID string) error { + if rc.Identifier == "" { + return ErrMissingAccountID + } + + if targetID == "" { + return ErrMissingTargetId + } + + uri := fmt.Sprintf( + "/%s/%s/infrastructure/targets/%s", + rc.Level, + rc.Identifier, + targetID, + ) + + _, err := api.makeRequestContext(ctx, http.MethodDelete, uri, nil) + if err != nil { + return fmt.Errorf("%s: %w", errMakeRequestError, err) + } + + return nil +} diff --git a/infrastructure_targets_test.go b/infrastructure_targets_test.go new file mode 100644 index 00000000000..5b587d9ec1f --- /dev/null +++ b/infrastructure_targets_test.go @@ -0,0 +1,287 @@ +package cloudflare + +import ( + "context" + "fmt" + "net/http" + "testing" + "time" + + "github.com/stretchr/testify/assert" +) + +// randomly generated uuid +const testInfrastructureTargetId = "019205b5-97d7-7272-b00e-0ea05e61a124" + +var ( + infrastrctureTargetCreatedOn, _ = time.Parse(time.RFC3339, "2024-08-25T05:00:22Z") + infrastrctureTargetModifiedOn, _ = time.Parse(time.RFC3339, "2024-08-25T05:00:22Z") + expectedInfrastructureTarget = Target{ + Hostname: "infra-access-target", + ID: testInfrastructureTargetId, + IP: IPInfo{ + IPV4: &IPDetails{ + IpAddr: "187.26.29.249", + VirtualNetworkId: "c77b744e-acc8-428f-9257-6878c046ed55", + }, + IPV6: &IPDetails{ + IpAddr: "64c0:64e8:f0b4:8dbf:7104:72b0:ec8f:f5e0", + VirtualNetworkId: "c77b744e-acc8-428f-9257-6878c046ed55", + }, + }, + CreatedAt: infrastrctureTargetCreatedOn, + ModifiedAt: infrastrctureTargetModifiedOn, + } + expectedInfrastructureModified = Target{ + Hostname: "infra-access-target-modified", + ID: testInfrastructureTargetId, + IP: IPInfo{ + IPV4: &IPDetails{ + IpAddr: "250.26.29.250", + VirtualNetworkId: "c77b744e-acc8-428f-9257-6878c046ed55", + }, + }, + CreatedAt: infrastrctureTargetCreatedOn, + ModifiedAt: infrastrctureTargetModifiedOn, + } +) + +func TestInfrastructureTarget_Create(t *testing.T) { + setup() + defer teardown() + + mux.HandleFunc("/accounts/"+testAccountID+"/infrastructure/targets", func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, http.MethodPost, r.Method, "Expected method 'POST', got %s", r.Method) + w.Header().Set("content-type", "application/json") + fmt.Fprint(w, ` + { + "success": true, + "errors": [], + "messages": [], + "result": { + "created_at": "2024-08-25T05:00:22Z", + "hostname": "infra-access-target", + "id": "019205b5-97d7-7272-b00e-0ea05e61a124", + "ip": { + "ipv4": { + "ip_addr": "187.26.29.249", + "virtual_network_id": "c77b744e-acc8-428f-9257-6878c046ed55" + }, + "ipv6": { + "ip_addr": "64c0:64e8:f0b4:8dbf:7104:72b0:ec8f:f5e0", + "virtual_network_id": "c77b744e-acc8-428f-9257-6878c046ed55" + } + }, + "modified_at": "2024-08-25T05:00:22Z" + } + }`) + }) + + // Make sure missing account ID is thrown + _, err := client.CreateInfrastructureTarget(context.Background(), AccountIdentifier(""), CreateInfrastructureTargetParams{}) + if assert.Error(t, err) { + assert.Equal(t, ErrMissingAccountID, err) + } + + out, err := client.CreateInfrastructureTarget(context.Background(), AccountIdentifier(testAccountID), CreateInfrastructureTargetParams{ + InfrastructureTargetParams: InfrastructureTargetParams{ + Hostname: "infra-access-target", + IP: IPInfo{ + IPV4: &IPDetails{ + IpAddr: "187.26.29.249", + VirtualNetworkId: "c77b744e-acc8-428f-9257-6878c046ed55", + }, + IPV6: &IPDetails{ + IpAddr: "64c0:64e8:f0b4:8dbf:7104:72b0:ec8f:f5e0", + VirtualNetworkId: "c77b744e-acc8-428f-9257-6878c046ed55", + }, + }, + }, + }) + if assert.NoError(t, err) { + assert.Equal(t, expectedInfrastructureTarget, out, "create infrastructure_target structs not equal") + } +} + +func TestInfrastructureTarget_List(t *testing.T) { + setup() + defer teardown() + + mux.HandleFunc("/accounts/"+testAccountID+"/infrastructure/targets", func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, http.MethodGet, r.Method, "Expected method 'GET', got %s", r.Method) + w.Header().Set("content-type", "application/json") + fmt.Fprint(w, ` +{ + "success": true, + "errors": [], + "messages": [], + "result": [ + { + "created_at": "2024-08-25T05:00:22Z", + "hostname": "infra-access-target", + "id": "019205b5-97d7-7272-b00e-0ea05e61a124", + "ip": { + "ipv4": { + "ip_addr": "187.26.29.249", + "virtual_network_id": "c77b744e-acc8-428f-9257-6878c046ed55" + }, + "ipv6": { + "ip_addr": "64c0:64e8:f0b4:8dbf:7104:72b0:ec8f:f5e0", + "virtual_network_id": "c77b744e-acc8-428f-9257-6878c046ed55" + } + }, + "modified_at": "2024-08-25T05:00:22Z" + } + ], + "result_info": { + "page": 1, + "per_page": 20, + "count": 1, + "total_count": 1 + } +}`) + }) + + _, _, err := client.ListInfrastructureTargets(context.Background(), AccountIdentifier(""), TargetListParams{}) + if assert.Error(t, err) { + assert.Equal(t, ErrMissingAccountID, err) + } + + out, results, err := client.ListInfrastructureTargets(context.Background(), AccountIdentifier(testAccountID), TargetListParams{}) + if assert.NoError(t, err) { + assert.Equal(t, 1, len(out), "expected 1 challenge_widgets") + assert.Equal(t, 20, results.PerPage, "expected 20 per page") + assert.Equal(t, expectedInfrastructureTarget, out[0], "list challenge_widgets structs not equal") + } +} + +func TestInfrastructureTarget_Get(t *testing.T) { + setup() + defer teardown() + + mux.HandleFunc("/accounts/"+testAccountID+"/infrastructure/targets/"+testInfrastructureTargetId, func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, http.MethodGet, r.Method, "Expected method 'GET', got %s", r.Method) + w.Header().Set("content-type", "application/json") + fmt.Fprint(w, ` +{ + "success": true, + "errors": [], + "messages": [], + "result": { + "created_at": "2024-08-25T05:00:22Z", + "hostname": "infra-access-target", + "id": "019205b5-97d7-7272-b00e-0ea05e61a124", + "ip": { + "ipv4": { + "ip_addr": "187.26.29.249", + "virtual_network_id": "c77b744e-acc8-428f-9257-6878c046ed55" + }, + "ipv6": { + "ip_addr": "64c0:64e8:f0b4:8dbf:7104:72b0:ec8f:f5e0", + "virtual_network_id": "c77b744e-acc8-428f-9257-6878c046ed55" + } + }, + "modified_at": "2024-08-25T05:00:22Z" + } +}`) + }) + + _, err := client.GetInfrastructureTarget(context.Background(), AccountIdentifier(""), "") + if assert.Error(t, err) { + assert.Equal(t, ErrMissingAccountID, err) + } + + _, err = client.GetInfrastructureTarget(context.Background(), AccountIdentifier(testAccountID), "") + if assert.Error(t, err) { + assert.Equal(t, ErrMissingTargetId, err) + } + + out, err := client.GetInfrastructureTarget(context.Background(), AccountIdentifier(testAccountID), testInfrastructureTargetId) + + if assert.NoError(t, err) { + assert.Equal(t, expectedInfrastructureTarget, out, "get infrastructure_target not equal to expected") + } +} + +func TestInfrastructureTarget_Update(t *testing.T) { + setup() + defer teardown() + + mux.HandleFunc("/accounts/"+testAccountID+"/infrastructure/targets/"+testInfrastructureTargetId, func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, http.MethodPut, r.Method, "Expected method 'PUT', got %s", r.Method) + w.Header().Set("content-type", "application/json") + fmt.Fprint(w, ` +{ + "success": true, + "errors": [], + "messages": [], + "result": { + "created_at": "2024-08-25T05:00:22Z", + "hostname": "infra-access-target-modified", + "id": "019205b5-97d7-7272-b00e-0ea05e61a124", + "ip": { + "ipv4": { + "ip_addr": "250.26.29.250", + "virtual_network_id": "c77b744e-acc8-428f-9257-6878c046ed55" + } + }, + "modified_at": "2024-08-25T05:00:22Z" + } +}`) + }) + + _, err := client.UpdateInfrastructureTarget(context.Background(), AccountIdentifier(""), UpdateInfrastructureTargetParams{}) + if assert.Error(t, err) { + assert.Equal(t, ErrMissingAccountID, err) + } + + _, err = client.UpdateInfrastructureTarget(context.Background(), AccountIdentifier(testAccountID), UpdateInfrastructureTargetParams{ + ID: "", + ModifyParams: InfrastructureTargetParams{}, + }) + if assert.Error(t, err) { + assert.Equal(t, ErrMissingTargetId, err) + } + + out, err := client.UpdateInfrastructureTarget(context.Background(), AccountIdentifier(testAccountID), UpdateInfrastructureTargetParams{ + ID: testInfrastructureTargetId, + ModifyParams: InfrastructureTargetParams{ + // Updates hostname and IPv4 address. Deletes IPv6 address. + Hostname: "infra-access-target-modified", + IP: IPInfo{ + IPV4: &IPDetails{ + IpAddr: "250.26.29.250", + VirtualNetworkId: "c77b744e-acc8-428f-9257-6878c046ed55", + }, + }, + }, + }) + if assert.NoError(t, err) { + assert.Equal(t, expectedInfrastructureModified, out, "update challenge_widgets structs not equal") + } +} + +func TestInfrastructureTarget_Delete(t *testing.T) { + setup() + defer teardown() + + mux.HandleFunc("/accounts/"+testAccountID+"/infrastructure/targets/"+testInfrastructureTargetId, func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, http.MethodDelete, r.Method, "Expected method 'DELETE', got %s", r.Method) + w.Header().Set("content-type", "application/json") + fmt.Fprint(w, ``) + }) + + // Make sure missing account ID is thrown + err := client.DeleteInfrastructureTarget(context.Background(), AccountIdentifier(""), "") + if assert.Error(t, err) { + assert.Equal(t, ErrMissingAccountID, err) + } + + err = client.DeleteInfrastructureTarget(context.Background(), AccountIdentifier(testAccountID), "") + if assert.Error(t, err) { + assert.Equal(t, ErrMissingTargetId, err) + } + + err = client.DeleteInfrastructureTarget(context.Background(), AccountIdentifier(testAccountID), testInfrastructureTargetId) + assert.NoError(t, err) +} From e35d0b357f1e43e7ad0bdd5f7fe8ba4eb8e3458d Mon Sep 17 00:00:00 2001 From: Sai Dadireddy Date: Thu, 19 Sep 2024 09:13:57 -0700 Subject: [PATCH 02/12] Update comments --- infrastructure_targets.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/infrastructure_targets.go b/infrastructure_targets.go index 7c6e5cb658a..3f4840e7385 100644 --- a/infrastructure_targets.go +++ b/infrastructure_targets.go @@ -12,7 +12,7 @@ import ( var ErrMissingTargetId = errors.New("required target id missing") -// AccessApplication represents an Access application. +// Target represents an Infrastructure Target. type Target struct { Hostname string `json:"hostname"` ID string `json:"id"` @@ -45,7 +45,7 @@ type UpdateInfrastructureTargetParams struct { ModifyParams InfrastructureTargetParams `json:"modify_params"` } -// TargetDetailResponse is the API response, containing a single access target. +// TargetDetailResponse is the API response, containing a single target. type TargetDetailResponse struct { Result Target `json:"result"` Response From 4b8e58d9847cef3892d80fdf720e844aa672ab6b Mon Sep 17 00:00:00 2001 From: Jacob Bednarz Date: Mon, 23 Sep 2024 12:32:05 +1000 Subject: [PATCH 03/12] s/IpAddr/IPAddr Signed-off-by: Jacob Bednarz --- infrastructure_targets.go | 12 ++++++------ infrastructure_targets_test.go | 12 ++++++------ 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/infrastructure_targets.go b/infrastructure_targets.go index 3f4840e7385..2560879e316 100644 --- a/infrastructure_targets.go +++ b/infrastructure_targets.go @@ -22,7 +22,7 @@ type Target struct { } type IPDetails struct { - IpAddr string `json:"ip_addr"` + IPAddr string `json:"ip_addr"` VirtualNetworkId string `json:"virtual_network_id"` } @@ -71,7 +71,7 @@ type TargetListParams struct { // ListInfrastructureTargets returns all applications within an account or zone. // -// Account API reference: https://developers.cloudflare.com/api/operations/infra-targets-list +// API reference: https://developers.cloudflare.com/api/operations/infra-targets-list func (api *API) ListInfrastructureTargets(ctx context.Context, rc *ResourceContainer, params TargetListParams) ([]Target, *ResultInfo, error) { if rc.Identifier == "" { return []Target{}, &ResultInfo{}, ErrMissingAccountID @@ -119,7 +119,7 @@ func (api *API) ListInfrastructureTargets(ctx context.Context, rc *ResourceConta // CreateInfrastructureTarget creates a new infrastructure target. // -// Account API reference: https://developers.cloudflare.com/api/operations/infra-targets-post +// API reference: https://developers.cloudflare.com/api/operations/infra-targets-post func (api *API) CreateInfrastructureTarget(ctx context.Context, rc *ResourceContainer, params CreateInfrastructureTargetParams) (Target, error) { if rc.Identifier == "" { return Target{}, ErrMissingAccountID @@ -143,7 +143,7 @@ func (api *API) CreateInfrastructureTarget(ctx context.Context, rc *ResourceCont // UpdateInfrastructureTarget updates an existing infrastructure target. // -// Account API reference: https://developers.cloudflare.com/api/operations/infra-targets-put +// API reference: https://developers.cloudflare.com/api/operations/infra-targets-put func (api *API) UpdateInfrastructureTarget(ctx context.Context, rc *ResourceContainer, params UpdateInfrastructureTargetParams) (Target, error) { if rc.Identifier == "" { return Target{}, ErrMissingAccountID @@ -177,7 +177,7 @@ func (api *API) UpdateInfrastructureTarget(ctx context.Context, rc *ResourceCont // GetInfrastructureTarget returns a single infrastructure target based on target ID // ID for either account or zone. // -// Account API reference: https://developers.cloudflare.com/api/operations/infra-targets-get +// API reference: https://developers.cloudflare.com/api/operations/infra-targets-get func (api *API) GetInfrastructureTarget(ctx context.Context, rc *ResourceContainer, targetID string) (Target, error) { if rc.Identifier == "" { return Target{}, ErrMissingAccountID @@ -210,7 +210,7 @@ func (api *API) GetInfrastructureTarget(ctx context.Context, rc *ResourceContain // DeleteInfrastructureTarget deletes an infrastructure target. // -// Account API reference: https://developers.cloudflare.com/api/operations/infra-targets-delete +// API reference: https://developers.cloudflare.com/api/operations/infra-targets-delete func (api *API) DeleteInfrastructureTarget(ctx context.Context, rc *ResourceContainer, targetID string) error { if rc.Identifier == "" { return ErrMissingAccountID diff --git a/infrastructure_targets_test.go b/infrastructure_targets_test.go index 5b587d9ec1f..7fd2bedfd28 100644 --- a/infrastructure_targets_test.go +++ b/infrastructure_targets_test.go @@ -21,11 +21,11 @@ var ( ID: testInfrastructureTargetId, IP: IPInfo{ IPV4: &IPDetails{ - IpAddr: "187.26.29.249", + IPAddr: "187.26.29.249", VirtualNetworkId: "c77b744e-acc8-428f-9257-6878c046ed55", }, IPV6: &IPDetails{ - IpAddr: "64c0:64e8:f0b4:8dbf:7104:72b0:ec8f:f5e0", + IPAddr: "64c0:64e8:f0b4:8dbf:7104:72b0:ec8f:f5e0", VirtualNetworkId: "c77b744e-acc8-428f-9257-6878c046ed55", }, }, @@ -37,7 +37,7 @@ var ( ID: testInfrastructureTargetId, IP: IPInfo{ IPV4: &IPDetails{ - IpAddr: "250.26.29.250", + IPAddr: "250.26.29.250", VirtualNetworkId: "c77b744e-acc8-428f-9257-6878c046ed55", }, }, @@ -88,11 +88,11 @@ func TestInfrastructureTarget_Create(t *testing.T) { Hostname: "infra-access-target", IP: IPInfo{ IPV4: &IPDetails{ - IpAddr: "187.26.29.249", + IPAddr: "187.26.29.249", VirtualNetworkId: "c77b744e-acc8-428f-9257-6878c046ed55", }, IPV6: &IPDetails{ - IpAddr: "64c0:64e8:f0b4:8dbf:7104:72b0:ec8f:f5e0", + IPAddr: "64c0:64e8:f0b4:8dbf:7104:72b0:ec8f:f5e0", VirtualNetworkId: "c77b744e-acc8-428f-9257-6878c046ed55", }, }, @@ -250,7 +250,7 @@ func TestInfrastructureTarget_Update(t *testing.T) { Hostname: "infra-access-target-modified", IP: IPInfo{ IPV4: &IPDetails{ - IpAddr: "250.26.29.250", + IPAddr: "250.26.29.250", VirtualNetworkId: "c77b744e-acc8-428f-9257-6878c046ed55", }, }, From 4d8d0bb43a3dcc4f48bfa236a91611425cd73fbb Mon Sep 17 00:00:00 2001 From: Jacob Bednarz Date: Mon, 23 Sep 2024 12:38:13 +1000 Subject: [PATCH 04/12] namespace all structs Signed-off-by: Jacob Bednarz --- infrastructure_targets.go | 84 +++++++++++++++++----------------- infrastructure_targets_test.go | 54 +++++++++++----------- 2 files changed, 69 insertions(+), 69 deletions(-) diff --git a/infrastructure_targets.go b/infrastructure_targets.go index 2560879e316..fbb4f319aaf 100644 --- a/infrastructure_targets.go +++ b/infrastructure_targets.go @@ -12,28 +12,28 @@ import ( var ErrMissingTargetId = errors.New("required target id missing") -// Target represents an Infrastructure Target. -type Target struct { - Hostname string `json:"hostname"` - ID string `json:"id"` - IP IPInfo `json:"ip"` - CreatedAt time.Time `json:"created_at"` - ModifiedAt time.Time `json:"modified_at"` +// InfrastructureTarget represents an Infrastructure InfrastructureTarget. +type InfrastructureTarget struct { + Hostname string `json:"hostname"` + ID string `json:"id"` + IP InfrastructureTargetIPInfo `json:"ip"` + CreatedAt time.Time `json:"created_at"` + ModifiedAt time.Time `json:"modified_at"` } -type IPDetails struct { +type InfrastructureTargetIPDetails struct { IPAddr string `json:"ip_addr"` VirtualNetworkId string `json:"virtual_network_id"` } -type IPInfo struct { - IPV4 *IPDetails `json:"ipv4,omitempty"` - IPV6 *IPDetails `json:"ipv6,omitempty"` +type InfrastructureTargetIPInfo struct { + IPV4 *InfrastructureTargetIPDetails `json:"ipv4,omitempty"` + IPV6 *InfrastructureTargetIPDetails `json:"ipv6,omitempty"` } type InfrastructureTargetParams struct { - Hostname string `json:"hostname"` - IP IPInfo `json:"ip"` + Hostname string `json:"hostname"` + IP InfrastructureTargetIPInfo `json:"ip"` } type CreateInfrastructureTargetParams struct { @@ -45,19 +45,19 @@ type UpdateInfrastructureTargetParams struct { ModifyParams InfrastructureTargetParams `json:"modify_params"` } -// TargetDetailResponse is the API response, containing a single target. -type TargetDetailResponse struct { - Result Target `json:"result"` +// InfrastructureTargetDetailResponse is the API response, containing a single target. +type InfrastructureTargetDetailResponse struct { + Result InfrastructureTarget `json:"result"` Response } -type TargetListDetailResponse struct { - Result []Target `json:"result"` +type InfrastructureTargetListDetailResponse struct { + Result []InfrastructureTarget `json:"result"` Response ResultInfo `json:"result_info"` } -type TargetListParams struct { +type InfrastructureTargetListParams struct { CreatedAfter string `url:"created_after,omitempty"` Hostname string `url:"hostname,omitempty"` HostnameContains string `url:"hostname_contains,omitempty"` @@ -72,9 +72,9 @@ type TargetListParams struct { // ListInfrastructureTargets returns all applications within an account or zone. // // API reference: https://developers.cloudflare.com/api/operations/infra-targets-list -func (api *API) ListInfrastructureTargets(ctx context.Context, rc *ResourceContainer, params TargetListParams) ([]Target, *ResultInfo, error) { +func (api *API) ListInfrastructureTargets(ctx context.Context, rc *ResourceContainer, params InfrastructureTargetListParams) ([]InfrastructureTarget, *ResultInfo, error) { if rc.Identifier == "" { - return []Target{}, &ResultInfo{}, ErrMissingAccountID + return []InfrastructureTarget{}, &ResultInfo{}, ErrMissingAccountID } baseURL := fmt.Sprintf("/%s/%s/infrastructure/targets", rc.Level, rc.Identifier) @@ -92,20 +92,20 @@ func (api *API) ListInfrastructureTargets(ctx context.Context, rc *ResourceConta params.Page = 1 } - var applications []Target - var r TargetListDetailResponse + var applications []InfrastructureTarget + var r InfrastructureTargetListDetailResponse for { uri := buildURI(baseURL, params) res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil) if err != nil { - return []Target{}, &ResultInfo{}, fmt.Errorf("%s: %w", errMakeRequestError, err) + return []InfrastructureTarget{}, &ResultInfo{}, fmt.Errorf("%s: %w", errMakeRequestError, err) } err = json.Unmarshal(res, &r) if err != nil { - return []Target{}, &ResultInfo{}, fmt.Errorf("%s: %w", errUnmarshalError, err) + return []InfrastructureTarget{}, &ResultInfo{}, fmt.Errorf("%s: %w", errUnmarshalError, err) } applications = append(applications, r.Result...) params.ResultInfo = r.ResultInfo.Next() @@ -120,22 +120,22 @@ func (api *API) ListInfrastructureTargets(ctx context.Context, rc *ResourceConta // CreateInfrastructureTarget creates a new infrastructure target. // // API reference: https://developers.cloudflare.com/api/operations/infra-targets-post -func (api *API) CreateInfrastructureTarget(ctx context.Context, rc *ResourceContainer, params CreateInfrastructureTargetParams) (Target, error) { +func (api *API) CreateInfrastructureTarget(ctx context.Context, rc *ResourceContainer, params CreateInfrastructureTargetParams) (InfrastructureTarget, error) { if rc.Identifier == "" { - return Target{}, ErrMissingAccountID + return InfrastructureTarget{}, ErrMissingAccountID } uri := fmt.Sprintf("/%s/%s/infrastructure/targets", rc.Level, rc.Identifier) res, err := api.makeRequestContext(ctx, http.MethodPost, uri, params) if err != nil { - return Target{}, fmt.Errorf("%s: %w", errMakeRequestError, err) + return InfrastructureTarget{}, fmt.Errorf("%s: %w", errMakeRequestError, err) } - var targetDetailResponse TargetDetailResponse + var targetDetailResponse InfrastructureTargetDetailResponse err = json.Unmarshal(res, &targetDetailResponse) if err != nil { - return Target{}, fmt.Errorf("%s: %w", errUnmarshalError, err) + return InfrastructureTarget{}, fmt.Errorf("%s: %w", errUnmarshalError, err) } return targetDetailResponse.Result, nil @@ -144,13 +144,13 @@ func (api *API) CreateInfrastructureTarget(ctx context.Context, rc *ResourceCont // UpdateInfrastructureTarget updates an existing infrastructure target. // // API reference: https://developers.cloudflare.com/api/operations/infra-targets-put -func (api *API) UpdateInfrastructureTarget(ctx context.Context, rc *ResourceContainer, params UpdateInfrastructureTargetParams) (Target, error) { +func (api *API) UpdateInfrastructureTarget(ctx context.Context, rc *ResourceContainer, params UpdateInfrastructureTargetParams) (InfrastructureTarget, error) { if rc.Identifier == "" { - return Target{}, ErrMissingAccountID + return InfrastructureTarget{}, ErrMissingAccountID } if params.ID == "" { - return Target{}, ErrMissingTargetId + return InfrastructureTarget{}, ErrMissingTargetId } uri := fmt.Sprintf( @@ -162,13 +162,13 @@ func (api *API) UpdateInfrastructureTarget(ctx context.Context, rc *ResourceCont res, err := api.makeRequestContext(ctx, http.MethodPut, uri, params.ModifyParams) if err != nil { - return Target{}, fmt.Errorf("%s: %w", errMakeRequestError, err) + return InfrastructureTarget{}, fmt.Errorf("%s: %w", errMakeRequestError, err) } - var targetDetailResponse TargetDetailResponse + var targetDetailResponse InfrastructureTargetDetailResponse err = json.Unmarshal(res, &targetDetailResponse) if err != nil { - return Target{}, fmt.Errorf("%s: %w", errUnmarshalError, err) + return InfrastructureTarget{}, fmt.Errorf("%s: %w", errUnmarshalError, err) } return targetDetailResponse.Result, nil @@ -178,13 +178,13 @@ func (api *API) UpdateInfrastructureTarget(ctx context.Context, rc *ResourceCont // ID for either account or zone. // // API reference: https://developers.cloudflare.com/api/operations/infra-targets-get -func (api *API) GetInfrastructureTarget(ctx context.Context, rc *ResourceContainer, targetID string) (Target, error) { +func (api *API) GetInfrastructureTarget(ctx context.Context, rc *ResourceContainer, targetID string) (InfrastructureTarget, error) { if rc.Identifier == "" { - return Target{}, ErrMissingAccountID + return InfrastructureTarget{}, ErrMissingAccountID } if targetID == "" { - return Target{}, ErrMissingTargetId + return InfrastructureTarget{}, ErrMissingTargetId } uri := fmt.Sprintf( @@ -196,13 +196,13 @@ func (api *API) GetInfrastructureTarget(ctx context.Context, rc *ResourceContain res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil) if err != nil { - return Target{}, fmt.Errorf("%s: %w", errMakeRequestError, err) + return InfrastructureTarget{}, fmt.Errorf("%s: %w", errMakeRequestError, err) } - var targetDetailResponse TargetDetailResponse + var targetDetailResponse InfrastructureTargetDetailResponse err = json.Unmarshal(res, &targetDetailResponse) if err != nil { - return Target{}, fmt.Errorf("%s: %w", errUnmarshalError, err) + return InfrastructureTarget{}, fmt.Errorf("%s: %w", errUnmarshalError, err) } return targetDetailResponse.Result, nil diff --git a/infrastructure_targets_test.go b/infrastructure_targets_test.go index 7fd2bedfd28..016d59ffb4a 100644 --- a/infrastructure_targets_test.go +++ b/infrastructure_targets_test.go @@ -16,28 +16,28 @@ const testInfrastructureTargetId = "019205b5-97d7-7272-b00e-0ea05e61a124" var ( infrastrctureTargetCreatedOn, _ = time.Parse(time.RFC3339, "2024-08-25T05:00:22Z") infrastrctureTargetModifiedOn, _ = time.Parse(time.RFC3339, "2024-08-25T05:00:22Z") - expectedInfrastructureTarget = Target{ + expectedInfrastructureTarget = InfrastructureTarget{ Hostname: "infra-access-target", ID: testInfrastructureTargetId, - IP: IPInfo{ - IPV4: &IPDetails{ - IPAddr: "187.26.29.249", + IP: InfrastructureTargetIPInfo{ + IPV4: &InfrastructureTargetIPDetails{ + IPAddr: "198.51.100.1", VirtualNetworkId: "c77b744e-acc8-428f-9257-6878c046ed55", }, - IPV6: &IPDetails{ - IPAddr: "64c0:64e8:f0b4:8dbf:7104:72b0:ec8f:f5e0", + IPV6: &InfrastructureTargetIPDetails{ + IPAddr: "2001:0db8:0000:0000:0000:0000:0000:1000", VirtualNetworkId: "c77b744e-acc8-428f-9257-6878c046ed55", }, }, CreatedAt: infrastrctureTargetCreatedOn, ModifiedAt: infrastrctureTargetModifiedOn, } - expectedInfrastructureModified = Target{ + expectedInfrastructureModified = InfrastructureTarget{ Hostname: "infra-access-target-modified", ID: testInfrastructureTargetId, - IP: IPInfo{ - IPV4: &IPDetails{ - IPAddr: "250.26.29.250", + IP: InfrastructureTargetIPInfo{ + IPV4: &InfrastructureTargetIPDetails{ + IPAddr: "198.51.100.2", VirtualNetworkId: "c77b744e-acc8-428f-9257-6878c046ed55", }, }, @@ -64,11 +64,11 @@ func TestInfrastructureTarget_Create(t *testing.T) { "id": "019205b5-97d7-7272-b00e-0ea05e61a124", "ip": { "ipv4": { - "ip_addr": "187.26.29.249", + "ip_addr": "198.51.100.1", "virtual_network_id": "c77b744e-acc8-428f-9257-6878c046ed55" }, "ipv6": { - "ip_addr": "64c0:64e8:f0b4:8dbf:7104:72b0:ec8f:f5e0", + "ip_addr": "2001:0db8:0000:0000:0000:0000:0000:1000", "virtual_network_id": "c77b744e-acc8-428f-9257-6878c046ed55" } }, @@ -86,13 +86,13 @@ func TestInfrastructureTarget_Create(t *testing.T) { out, err := client.CreateInfrastructureTarget(context.Background(), AccountIdentifier(testAccountID), CreateInfrastructureTargetParams{ InfrastructureTargetParams: InfrastructureTargetParams{ Hostname: "infra-access-target", - IP: IPInfo{ - IPV4: &IPDetails{ - IPAddr: "187.26.29.249", + IP: InfrastructureTargetIPInfo{ + IPV4: &InfrastructureTargetIPDetails{ + IPAddr: "198.51.100.1", VirtualNetworkId: "c77b744e-acc8-428f-9257-6878c046ed55", }, - IPV6: &IPDetails{ - IPAddr: "64c0:64e8:f0b4:8dbf:7104:72b0:ec8f:f5e0", + IPV6: &InfrastructureTargetIPDetails{ + IPAddr: "2001:0db8:0000:0000:0000:0000:0000:1000", VirtualNetworkId: "c77b744e-acc8-428f-9257-6878c046ed55", }, }, @@ -122,11 +122,11 @@ func TestInfrastructureTarget_List(t *testing.T) { "id": "019205b5-97d7-7272-b00e-0ea05e61a124", "ip": { "ipv4": { - "ip_addr": "187.26.29.249", + "ip_addr": "198.51.100.1", "virtual_network_id": "c77b744e-acc8-428f-9257-6878c046ed55" }, "ipv6": { - "ip_addr": "64c0:64e8:f0b4:8dbf:7104:72b0:ec8f:f5e0", + "ip_addr": "2001:0db8:0000:0000:0000:0000:0000:1000", "virtual_network_id": "c77b744e-acc8-428f-9257-6878c046ed55" } }, @@ -142,12 +142,12 @@ func TestInfrastructureTarget_List(t *testing.T) { }`) }) - _, _, err := client.ListInfrastructureTargets(context.Background(), AccountIdentifier(""), TargetListParams{}) + _, _, err := client.ListInfrastructureTargets(context.Background(), AccountIdentifier(""), InfrastructureTargetListParams{}) if assert.Error(t, err) { assert.Equal(t, ErrMissingAccountID, err) } - out, results, err := client.ListInfrastructureTargets(context.Background(), AccountIdentifier(testAccountID), TargetListParams{}) + out, results, err := client.ListInfrastructureTargets(context.Background(), AccountIdentifier(testAccountID), InfrastructureTargetListParams{}) if assert.NoError(t, err) { assert.Equal(t, 1, len(out), "expected 1 challenge_widgets") assert.Equal(t, 20, results.PerPage, "expected 20 per page") @@ -173,11 +173,11 @@ func TestInfrastructureTarget_Get(t *testing.T) { "id": "019205b5-97d7-7272-b00e-0ea05e61a124", "ip": { "ipv4": { - "ip_addr": "187.26.29.249", + "ip_addr": "198.51.100.1", "virtual_network_id": "c77b744e-acc8-428f-9257-6878c046ed55" }, "ipv6": { - "ip_addr": "64c0:64e8:f0b4:8dbf:7104:72b0:ec8f:f5e0", + "ip_addr": "2001:0db8:0000:0000:0000:0000:0000:1000", "virtual_network_id": "c77b744e-acc8-428f-9257-6878c046ed55" } }, @@ -221,7 +221,7 @@ func TestInfrastructureTarget_Update(t *testing.T) { "id": "019205b5-97d7-7272-b00e-0ea05e61a124", "ip": { "ipv4": { - "ip_addr": "250.26.29.250", + "ip_addr": "198.51.100.2", "virtual_network_id": "c77b744e-acc8-428f-9257-6878c046ed55" } }, @@ -248,9 +248,9 @@ func TestInfrastructureTarget_Update(t *testing.T) { ModifyParams: InfrastructureTargetParams{ // Updates hostname and IPv4 address. Deletes IPv6 address. Hostname: "infra-access-target-modified", - IP: IPInfo{ - IPV4: &IPDetails{ - IPAddr: "250.26.29.250", + IP: InfrastructureTargetIPInfo{ + IPV4: &InfrastructureTargetIPDetails{ + IPAddr: "198.51.100.2", VirtualNetworkId: "c77b744e-acc8-428f-9257-6878c046ed55", }, }, From 124d2f681ff7907092d474aa149a7b274bdec11c Mon Sep 17 00:00:00 2001 From: Jacob Bednarz Date: Mon, 23 Sep 2024 12:38:57 +1000 Subject: [PATCH 05/12] don't marshal `id` into the payload --- infrastructure_targets.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/infrastructure_targets.go b/infrastructure_targets.go index fbb4f319aaf..1200da117f7 100644 --- a/infrastructure_targets.go +++ b/infrastructure_targets.go @@ -41,7 +41,7 @@ type CreateInfrastructureTargetParams struct { } type UpdateInfrastructureTargetParams struct { - ID string `json:"hostname"` + ID string `json:"-"` ModifyParams InfrastructureTargetParams `json:"modify_params"` } From 0f00db2a6af06aebdb3e6d4321e65496ee403937 Mon Sep 17 00:00:00 2001 From: Jacob Bednarz Date: Mon, 23 Sep 2024 12:43:02 +1000 Subject: [PATCH 06/12] fix lint --- infrastructure_targets.go | 1 - infrastructure_targets_test.go | 1 - 2 files changed, 2 deletions(-) diff --git a/infrastructure_targets.go b/infrastructure_targets.go index 1200da117f7..5dba32d0273 100644 --- a/infrastructure_targets.go +++ b/infrastructure_targets.go @@ -12,7 +12,6 @@ import ( var ErrMissingTargetId = errors.New("required target id missing") -// InfrastructureTarget represents an Infrastructure InfrastructureTarget. type InfrastructureTarget struct { Hostname string `json:"hostname"` ID string `json:"id"` diff --git a/infrastructure_targets_test.go b/infrastructure_targets_test.go index 016d59ffb4a..01f067cc085 100644 --- a/infrastructure_targets_test.go +++ b/infrastructure_targets_test.go @@ -10,7 +10,6 @@ import ( "github.com/stretchr/testify/assert" ) -// randomly generated uuid const testInfrastructureTargetId = "019205b5-97d7-7272-b00e-0ea05e61a124" var ( From de28b77d2f33a94a36e2c009ec3c186b394bcddc Mon Sep 17 00:00:00 2001 From: Jacob Bednarz Date: Mon, 23 Sep 2024 12:44:52 +1000 Subject: [PATCH 07/12] Rename 3183.txt to 3184.txt --- .changelog/{3183.txt => 3184.txt} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename .changelog/{3183.txt => 3184.txt} (96%) diff --git a/.changelog/3183.txt b/.changelog/3184.txt similarity index 96% rename from .changelog/3183.txt rename to .changelog/3184.txt index 69e4ec2d5eb..e0c858c3517 100644 --- a/.changelog/3183.txt +++ b/.changelog/3184.txt @@ -1,3 +1,3 @@ ```release-note:enhancement infrastructure_targets: initialize CRUD endpoints for infrastructure access endpoints -``` \ No newline at end of file +``` From 52ef9acb8e9d403a5f0b734dd14cb1847cbc5573 Mon Sep 17 00:00:00 2001 From: Jacob Bednarz Date: Mon, 23 Sep 2024 12:52:13 +1000 Subject: [PATCH 08/12] swap time.Time to pointers --- infrastructure_targets.go | 4 ++-- infrastructure_targets_test.go | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/infrastructure_targets.go b/infrastructure_targets.go index 5dba32d0273..c6026798ef6 100644 --- a/infrastructure_targets.go +++ b/infrastructure_targets.go @@ -16,8 +16,8 @@ type InfrastructureTarget struct { Hostname string `json:"hostname"` ID string `json:"id"` IP InfrastructureTargetIPInfo `json:"ip"` - CreatedAt time.Time `json:"created_at"` - ModifiedAt time.Time `json:"modified_at"` + CreatedAt *time.Time `json:"created_at"` + ModifiedAt *time.Time `json:"modified_at"` } type InfrastructureTargetIPDetails struct { diff --git a/infrastructure_targets_test.go b/infrastructure_targets_test.go index 01f067cc085..fd60ee2cbc3 100644 --- a/infrastructure_targets_test.go +++ b/infrastructure_targets_test.go @@ -28,8 +28,8 @@ var ( VirtualNetworkId: "c77b744e-acc8-428f-9257-6878c046ed55", }, }, - CreatedAt: infrastrctureTargetCreatedOn, - ModifiedAt: infrastrctureTargetModifiedOn, + CreatedAt: &infrastrctureTargetCreatedOn, + ModifiedAt: &infrastrctureTargetModifiedOn, } expectedInfrastructureModified = InfrastructureTarget{ Hostname: "infra-access-target-modified", @@ -40,8 +40,8 @@ var ( VirtualNetworkId: "c77b744e-acc8-428f-9257-6878c046ed55", }, }, - CreatedAt: infrastrctureTargetCreatedOn, - ModifiedAt: infrastrctureTargetModifiedOn, + CreatedAt: &infrastrctureTargetCreatedOn, + ModifiedAt: &infrastrctureTargetModifiedOn, } ) From 91b43278b1fd0c58648f04944161e36d5bee57e4 Mon Sep 17 00:00:00 2001 From: Sai Dadireddy Date: Sun, 22 Sep 2024 20:32:31 -0700 Subject: [PATCH 09/12] Rename Infrastructure Target to Infrastructure Access Target --- infrastructure_access_targets.go | 234 +++++++++++++++++ ...o => infrastructure_access_targets_test.go | 93 +++---- infrastructure_targets.go | 235 ------------------ 3 files changed, 281 insertions(+), 281 deletions(-) create mode 100644 infrastructure_access_targets.go rename infrastructure_targets_test.go => infrastructure_access_targets_test.go (61%) delete mode 100644 infrastructure_targets.go diff --git a/infrastructure_access_targets.go b/infrastructure_access_targets.go new file mode 100644 index 00000000000..0c6a72a8666 --- /dev/null +++ b/infrastructure_access_targets.go @@ -0,0 +1,234 @@ +package cloudflare + +import ( + "context" + "errors" + "fmt" + "net/http" + + "github.com/goccy/go-json" +) + +var ErrMissingTargetId = errors.New("required target id missing") + +type InfrastructureAccessTarget struct { + Hostname string `json:"hostname"` + ID string `json:"id"` + IP InfrastructureAccessTargetIPInfo `json:"ip"` + CreatedAt string `json:"created_at"` + ModifiedAt string `json:"modified_at"` +} + +type InfrastructureAccessTargetIPInfo struct { + IPV4 *InfrastructureAccessTargetIPDetails `json:"ipv4,omitempty"` + IPV6 *InfrastructureAccessTargetIPDetails `json:"ipv6,omitempty"` +} + +type InfrastructureAccessTargetIPDetails struct { + IPAddr string `json:"ip_addr"` + VirtualNetworkId string `json:"virtual_network_id"` +} + +type InfrastructureAccessTargetParams struct { + Hostname string `json:"hostname"` + IP InfrastructureAccessTargetIPInfo `json:"ip"` +} + +type CreateInfrastructureAccessTargetParams struct { + InfrastructureAccessTargetParams +} + +type UpdateInfrastructureAccessTargetParams struct { + ID string `json:"-"` + ModifyParams InfrastructureAccessTargetParams `json:"modify_params"` +} + +// InfrastructureAccessTargetDetailResponse is the API response, containing a single target. +type InfrastructureAccessTargetDetailResponse struct { + Result InfrastructureAccessTarget `json:"result"` + Response +} + +type InfrastructureAccessTargetListDetailResponse struct { + Result []InfrastructureAccessTarget `json:"result"` + Response + ResultInfo `json:"result_info"` +} + +type InfrastructureAccessTargetListParams struct { + Hostname string `url:"hostname,omitempty"` + HostnameContains string `url:"hostname_contains,omitempty"` + IPV4 string `url:"ip_v4,omitempty"` + IPV6 string `url:"ip_v6,omitempty"` + CreatedAfter string `url:"created_after,omitempty"` + ModifedAfter string `url:"modified_after,omitempty"` + VirtualNetworkId string `url:"virtual_network_id,omitempty"` + + ResultInfo +} + +// ListInfrastructureAccessTargets returns all infrastructure access targets within an account. +// +// Account API reference: https://developers.cloudflare.com/api/operations/infra-targets-list +func (api *API) ListInfrastructureAccessTargets(ctx context.Context, rc *ResourceContainer, params InfrastructureAccessTargetListParams) ([]InfrastructureAccessTarget, *ResultInfo, error) { + if rc.Identifier == "" { + return []InfrastructureAccessTarget{}, &ResultInfo{}, ErrMissingAccountID + } + + baseURL := fmt.Sprintf("/%s/%s/infrastructure/targets", rc.Level, rc.Identifier) + + autoPaginate := true + if params.PerPage >= 1 || params.Page >= 1 { + autoPaginate = false + } + + if params.PerPage < 1 { + params.PerPage = 25 + } + + if params.Page < 1 { + params.Page = 1 + } + + var targets []InfrastructureAccessTarget + var r InfrastructureAccessTargetListDetailResponse + + for { + uri := buildURI(baseURL, params) + + res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil) + if err != nil { + return []InfrastructureAccessTarget{}, &ResultInfo{}, fmt.Errorf("%s: %w", errMakeRequestError, err) + } + + err = json.Unmarshal(res, &r) + if err != nil { + return []InfrastructureAccessTarget{}, &ResultInfo{}, fmt.Errorf("%s: %w", errUnmarshalError, err) + } + targets = append(targets, r.Result...) + params.ResultInfo = r.ResultInfo.Next() + if params.ResultInfo.Done() || !autoPaginate { + break + } + } + + return targets, &r.ResultInfo, nil +} + +// CreateInfrastructureAccessTarget creates a new infrastructure access target. +// +// Account API reference: https://developers.cloudflare.com/api/operations/infra-targets-post +func (api *API) CreateInfrastructureAccessTarget(ctx context.Context, rc *ResourceContainer, params CreateInfrastructureAccessTargetParams) (InfrastructureAccessTarget, error) { + if rc.Identifier == "" { + return InfrastructureAccessTarget{}, ErrMissingAccountID + } + + uri := fmt.Sprintf("/%s/%s/infrastructure/targets", rc.Level, rc.Identifier) + + res, err := api.makeRequestContext(ctx, http.MethodPost, uri, params) + if err != nil { + return InfrastructureAccessTarget{}, fmt.Errorf("%s: %w", errMakeRequestError, err) + } + + var targetDetailResponse InfrastructureAccessTargetDetailResponse + err = json.Unmarshal(res, &targetDetailResponse) + if err != nil { + return InfrastructureAccessTarget{}, fmt.Errorf("%s: %w", errUnmarshalError, err) + } + + return targetDetailResponse.Result, nil +} + +// UpdateInfrastructureAccessTarget updates an existing infrastructure access target. +// +// Account API reference: https://developers.cloudflare.com/api/operations/infra-targets-put +func (api *API) UpdateInfrastructureAccessTarget(ctx context.Context, rc *ResourceContainer, params UpdateInfrastructureAccessTargetParams) (InfrastructureAccessTarget, error) { + if rc.Identifier == "" { + return InfrastructureAccessTarget{}, ErrMissingAccountID + } + + if params.ID == "" { + return InfrastructureAccessTarget{}, ErrMissingTargetId + } + + uri := fmt.Sprintf( + "/%s/%s/infrastructure/targets/%s", + rc.Level, + rc.Identifier, + params.ID, + ) + + res, err := api.makeRequestContext(ctx, http.MethodPut, uri, params.ModifyParams) + if err != nil { + return InfrastructureAccessTarget{}, fmt.Errorf("%s: %w", errMakeRequestError, err) + } + + var targetDetailResponse InfrastructureAccessTargetDetailResponse + err = json.Unmarshal(res, &targetDetailResponse) + if err != nil { + return InfrastructureAccessTarget{}, fmt.Errorf("%s: %w", errUnmarshalError, err) + } + + return targetDetailResponse.Result, nil +} + +// GetInfrastructureAccessTarget returns a single infrastructure access target based on target ID +// ID for either account or zone. +// +// Account API reference: https://developers.cloudflare.com/api/operations/infra-targets-get +func (api *API) GetInfrastructureAccessTarget(ctx context.Context, rc *ResourceContainer, targetID string) (InfrastructureAccessTarget, error) { + if rc.Identifier == "" { + return InfrastructureAccessTarget{}, ErrMissingAccountID + } + + if targetID == "" { + return InfrastructureAccessTarget{}, ErrMissingTargetId + } + + uri := fmt.Sprintf( + "/%s/%s/infrastructure/targets/%s", + rc.Level, + rc.Identifier, + targetID, + ) + + res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil) + if err != nil { + return InfrastructureAccessTarget{}, fmt.Errorf("%s: %w", errMakeRequestError, err) + } + + var targetDetailResponse InfrastructureAccessTargetDetailResponse + err = json.Unmarshal(res, &targetDetailResponse) + if err != nil { + return InfrastructureAccessTarget{}, fmt.Errorf("%s: %w", errUnmarshalError, err) + } + + return targetDetailResponse.Result, nil +} + +// DeleteInfrastructureAccessTarget deletes an infrastructure access target. +// +// Account API reference: https://developers.cloudflare.com/api/operations/infra-targets-delete +func (api *API) DeleteInfrastructureAccessTarget(ctx context.Context, rc *ResourceContainer, targetID string) error { + if rc.Identifier == "" { + return ErrMissingAccountID + } + + if targetID == "" { + return ErrMissingTargetId + } + + uri := fmt.Sprintf( + "/%s/%s/infrastructure/targets/%s", + rc.Level, + rc.Identifier, + targetID, + ) + + _, err := api.makeRequestContext(ctx, http.MethodDelete, uri, nil) + if err != nil { + return fmt.Errorf("%s: %w", errMakeRequestError, err) + } + + return nil +} diff --git a/infrastructure_targets_test.go b/infrastructure_access_targets_test.go similarity index 61% rename from infrastructure_targets_test.go rename to infrastructure_access_targets_test.go index fd60ee2cbc3..f00c334c7f0 100644 --- a/infrastructure_targets_test.go +++ b/infrastructure_access_targets_test.go @@ -10,38 +10,39 @@ import ( "github.com/stretchr/testify/assert" ) -const testInfrastructureTargetId = "019205b5-97d7-7272-b00e-0ea05e61a124" +// randomly generated uuid +const testInfrastructureAccessTargetId = "019205b5-97d7-7272-b00e-0ea05e61a124" var ( - infrastrctureTargetCreatedOn, _ = time.Parse(time.RFC3339, "2024-08-25T05:00:22Z") - infrastrctureTargetModifiedOn, _ = time.Parse(time.RFC3339, "2024-08-25T05:00:22Z") - expectedInfrastructureTarget = InfrastructureTarget{ + infrastructureAccessTargetCreatedOn, _ = time.Parse(time.RFC3339, "2024-08-25T05:00:22Z") + infrastructureAccessTargetModifiedOn, _ = time.Parse(time.RFC3339, "2024-08-25T05:00:22Z") + expectedInfrastructureAccessTarget = InfrastructureAccessTarget{ Hostname: "infra-access-target", - ID: testInfrastructureTargetId, - IP: InfrastructureTargetIPInfo{ - IPV4: &InfrastructureTargetIPDetails{ - IPAddr: "198.51.100.1", + ID: testInfrastructureAccessTargetId, + IP: InfrastructureAccessTargetIPInfo{ + IPV4: &InfrastructureAccessTargetIPDetails{ + IPAddr: "187.26.29.249", VirtualNetworkId: "c77b744e-acc8-428f-9257-6878c046ed55", }, - IPV6: &InfrastructureTargetIPDetails{ + IPV6: &InfrastructureAccessTargetIPDetails{ IPAddr: "2001:0db8:0000:0000:0000:0000:0000:1000", VirtualNetworkId: "c77b744e-acc8-428f-9257-6878c046ed55", }, }, - CreatedAt: &infrastrctureTargetCreatedOn, - ModifiedAt: &infrastrctureTargetModifiedOn, + CreatedAt: infrastructureAccessTargetCreatedOn.String(), + ModifiedAt: infrastructureAccessTargetModifiedOn.String(), } - expectedInfrastructureModified = InfrastructureTarget{ + expectedInfrastructureModified = InfrastructureAccessTarget{ Hostname: "infra-access-target-modified", - ID: testInfrastructureTargetId, - IP: InfrastructureTargetIPInfo{ - IPV4: &InfrastructureTargetIPDetails{ - IPAddr: "198.51.100.2", + ID: testInfrastructureAccessTargetId, + IP: InfrastructureAccessTargetIPInfo{ + IPV4: &InfrastructureAccessTargetIPDetails{ + IPAddr: "250.26.29.250", VirtualNetworkId: "c77b744e-acc8-428f-9257-6878c046ed55", }, }, - CreatedAt: &infrastrctureTargetCreatedOn, - ModifiedAt: &infrastrctureTargetModifiedOn, + CreatedAt: infrastructureAccessTargetCreatedOn.String(), + ModifiedAt: infrastructureAccessTargetModifiedOn.String(), } ) @@ -77,20 +78,20 @@ func TestInfrastructureTarget_Create(t *testing.T) { }) // Make sure missing account ID is thrown - _, err := client.CreateInfrastructureTarget(context.Background(), AccountIdentifier(""), CreateInfrastructureTargetParams{}) + _, err := client.CreateInfrastructureAccessTarget(context.Background(), AccountIdentifier(""), CreateInfrastructureAccessTargetParams{}) if assert.Error(t, err) { assert.Equal(t, ErrMissingAccountID, err) } - out, err := client.CreateInfrastructureTarget(context.Background(), AccountIdentifier(testAccountID), CreateInfrastructureTargetParams{ - InfrastructureTargetParams: InfrastructureTargetParams{ + out, err := client.CreateInfrastructureAccessTarget(context.Background(), AccountIdentifier(testAccountID), CreateInfrastructureAccessTargetParams{ + InfrastructureAccessTargetParams: InfrastructureAccessTargetParams{ Hostname: "infra-access-target", - IP: InfrastructureTargetIPInfo{ - IPV4: &InfrastructureTargetIPDetails{ + IP: InfrastructureAccessTargetIPInfo{ + IPV4: &InfrastructureAccessTargetIPDetails{ IPAddr: "198.51.100.1", VirtualNetworkId: "c77b744e-acc8-428f-9257-6878c046ed55", }, - IPV6: &InfrastructureTargetIPDetails{ + IPV6: &InfrastructureAccessTargetIPDetails{ IPAddr: "2001:0db8:0000:0000:0000:0000:0000:1000", VirtualNetworkId: "c77b744e-acc8-428f-9257-6878c046ed55", }, @@ -98,7 +99,7 @@ func TestInfrastructureTarget_Create(t *testing.T) { }, }) if assert.NoError(t, err) { - assert.Equal(t, expectedInfrastructureTarget, out, "create infrastructure_target structs not equal") + assert.Equal(t, expectedInfrastructureAccessTarget, out, "create infrastructure_target structs not equal") } } @@ -141,16 +142,16 @@ func TestInfrastructureTarget_List(t *testing.T) { }`) }) - _, _, err := client.ListInfrastructureTargets(context.Background(), AccountIdentifier(""), InfrastructureTargetListParams{}) + _, _, err := client.ListInfrastructureAccessTargets(context.Background(), AccountIdentifier(""), InfrastructureAccessTargetListParams{}) if assert.Error(t, err) { assert.Equal(t, ErrMissingAccountID, err) } - out, results, err := client.ListInfrastructureTargets(context.Background(), AccountIdentifier(testAccountID), InfrastructureTargetListParams{}) + out, results, err := client.ListInfrastructureAccessTargets(context.Background(), AccountIdentifier(testAccountID), InfrastructureAccessTargetListParams{}) if assert.NoError(t, err) { assert.Equal(t, 1, len(out), "expected 1 challenge_widgets") assert.Equal(t, 20, results.PerPage, "expected 20 per page") - assert.Equal(t, expectedInfrastructureTarget, out[0], "list challenge_widgets structs not equal") + assert.Equal(t, expectedInfrastructureAccessTarget, out[0], "list challenge_widgets structs not equal") } } @@ -158,7 +159,7 @@ func TestInfrastructureTarget_Get(t *testing.T) { setup() defer teardown() - mux.HandleFunc("/accounts/"+testAccountID+"/infrastructure/targets/"+testInfrastructureTargetId, func(w http.ResponseWriter, r *http.Request) { + mux.HandleFunc("/accounts/"+testAccountID+"/infrastructure/targets/"+testInfrastructureAccessTargetId, func(w http.ResponseWriter, r *http.Request) { assert.Equal(t, http.MethodGet, r.Method, "Expected method 'GET', got %s", r.Method) w.Header().Set("content-type", "application/json") fmt.Fprint(w, ` @@ -185,20 +186,20 @@ func TestInfrastructureTarget_Get(t *testing.T) { }`) }) - _, err := client.GetInfrastructureTarget(context.Background(), AccountIdentifier(""), "") + _, err := client.GetInfrastructureAccessTarget(context.Background(), AccountIdentifier(""), "") if assert.Error(t, err) { assert.Equal(t, ErrMissingAccountID, err) } - _, err = client.GetInfrastructureTarget(context.Background(), AccountIdentifier(testAccountID), "") + _, err = client.GetInfrastructureAccessTarget(context.Background(), AccountIdentifier(testAccountID), "") if assert.Error(t, err) { assert.Equal(t, ErrMissingTargetId, err) } - out, err := client.GetInfrastructureTarget(context.Background(), AccountIdentifier(testAccountID), testInfrastructureTargetId) + out, err := client.GetInfrastructureAccessTarget(context.Background(), AccountIdentifier(testAccountID), testInfrastructureAccessTargetId) if assert.NoError(t, err) { - assert.Equal(t, expectedInfrastructureTarget, out, "get infrastructure_target not equal to expected") + assert.Equal(t, expectedInfrastructureAccessTarget, out, "get infrastructure_target not equal to expected") } } @@ -206,7 +207,7 @@ func TestInfrastructureTarget_Update(t *testing.T) { setup() defer teardown() - mux.HandleFunc("/accounts/"+testAccountID+"/infrastructure/targets/"+testInfrastructureTargetId, func(w http.ResponseWriter, r *http.Request) { + mux.HandleFunc("/accounts/"+testAccountID+"/infrastructure/targets/"+testInfrastructureAccessTargetId, func(w http.ResponseWriter, r *http.Request) { assert.Equal(t, http.MethodPut, r.Method, "Expected method 'PUT', got %s", r.Method) w.Header().Set("content-type", "application/json") fmt.Fprint(w, ` @@ -229,26 +230,26 @@ func TestInfrastructureTarget_Update(t *testing.T) { }`) }) - _, err := client.UpdateInfrastructureTarget(context.Background(), AccountIdentifier(""), UpdateInfrastructureTargetParams{}) + _, err := client.UpdateInfrastructureAccessTarget(context.Background(), AccountIdentifier(""), UpdateInfrastructureAccessTargetParams{}) if assert.Error(t, err) { assert.Equal(t, ErrMissingAccountID, err) } - _, err = client.UpdateInfrastructureTarget(context.Background(), AccountIdentifier(testAccountID), UpdateInfrastructureTargetParams{ + _, err = client.UpdateInfrastructureAccessTarget(context.Background(), AccountIdentifier(testAccountID), UpdateInfrastructureAccessTargetParams{ ID: "", - ModifyParams: InfrastructureTargetParams{}, + ModifyParams: InfrastructureAccessTargetParams{}, }) if assert.Error(t, err) { assert.Equal(t, ErrMissingTargetId, err) } - out, err := client.UpdateInfrastructureTarget(context.Background(), AccountIdentifier(testAccountID), UpdateInfrastructureTargetParams{ - ID: testInfrastructureTargetId, - ModifyParams: InfrastructureTargetParams{ + out, err := client.UpdateInfrastructureAccessTarget(context.Background(), AccountIdentifier(testAccountID), UpdateInfrastructureAccessTargetParams{ + ID: testInfrastructureAccessTargetId, + ModifyParams: InfrastructureAccessTargetParams{ // Updates hostname and IPv4 address. Deletes IPv6 address. Hostname: "infra-access-target-modified", - IP: InfrastructureTargetIPInfo{ - IPV4: &InfrastructureTargetIPDetails{ + IP: InfrastructureAccessTargetIPInfo{ + IPV4: &InfrastructureAccessTargetIPDetails{ IPAddr: "198.51.100.2", VirtualNetworkId: "c77b744e-acc8-428f-9257-6878c046ed55", }, @@ -264,23 +265,23 @@ func TestInfrastructureTarget_Delete(t *testing.T) { setup() defer teardown() - mux.HandleFunc("/accounts/"+testAccountID+"/infrastructure/targets/"+testInfrastructureTargetId, func(w http.ResponseWriter, r *http.Request) { + mux.HandleFunc("/accounts/"+testAccountID+"/infrastructure/targets/"+testInfrastructureAccessTargetId, func(w http.ResponseWriter, r *http.Request) { assert.Equal(t, http.MethodDelete, r.Method, "Expected method 'DELETE', got %s", r.Method) w.Header().Set("content-type", "application/json") fmt.Fprint(w, ``) }) // Make sure missing account ID is thrown - err := client.DeleteInfrastructureTarget(context.Background(), AccountIdentifier(""), "") + err := client.DeleteInfrastructureAccessTarget(context.Background(), AccountIdentifier(""), "") if assert.Error(t, err) { assert.Equal(t, ErrMissingAccountID, err) } - err = client.DeleteInfrastructureTarget(context.Background(), AccountIdentifier(testAccountID), "") + err = client.DeleteInfrastructureAccessTarget(context.Background(), AccountIdentifier(testAccountID), "") if assert.Error(t, err) { assert.Equal(t, ErrMissingTargetId, err) } - err = client.DeleteInfrastructureTarget(context.Background(), AccountIdentifier(testAccountID), testInfrastructureTargetId) + err = client.DeleteInfrastructureAccessTarget(context.Background(), AccountIdentifier(testAccountID), testInfrastructureAccessTargetId) assert.NoError(t, err) } diff --git a/infrastructure_targets.go b/infrastructure_targets.go deleted file mode 100644 index c6026798ef6..00000000000 --- a/infrastructure_targets.go +++ /dev/null @@ -1,235 +0,0 @@ -package cloudflare - -import ( - "context" - "errors" - "fmt" - "net/http" - "time" - - "github.com/goccy/go-json" -) - -var ErrMissingTargetId = errors.New("required target id missing") - -type InfrastructureTarget struct { - Hostname string `json:"hostname"` - ID string `json:"id"` - IP InfrastructureTargetIPInfo `json:"ip"` - CreatedAt *time.Time `json:"created_at"` - ModifiedAt *time.Time `json:"modified_at"` -} - -type InfrastructureTargetIPDetails struct { - IPAddr string `json:"ip_addr"` - VirtualNetworkId string `json:"virtual_network_id"` -} - -type InfrastructureTargetIPInfo struct { - IPV4 *InfrastructureTargetIPDetails `json:"ipv4,omitempty"` - IPV6 *InfrastructureTargetIPDetails `json:"ipv6,omitempty"` -} - -type InfrastructureTargetParams struct { - Hostname string `json:"hostname"` - IP InfrastructureTargetIPInfo `json:"ip"` -} - -type CreateInfrastructureTargetParams struct { - InfrastructureTargetParams -} - -type UpdateInfrastructureTargetParams struct { - ID string `json:"-"` - ModifyParams InfrastructureTargetParams `json:"modify_params"` -} - -// InfrastructureTargetDetailResponse is the API response, containing a single target. -type InfrastructureTargetDetailResponse struct { - Result InfrastructureTarget `json:"result"` - Response -} - -type InfrastructureTargetListDetailResponse struct { - Result []InfrastructureTarget `json:"result"` - Response - ResultInfo `json:"result_info"` -} - -type InfrastructureTargetListParams struct { - CreatedAfter string `url:"created_after,omitempty"` - Hostname string `url:"hostname,omitempty"` - HostnameContains string `url:"hostname_contains,omitempty"` - IPV4 string `url:"ip_v4,omitempty"` - IPV6 string `url:"ip_v6,omitempty"` - ModifedAfter string `url:"modified_after,omitempty"` - VirtualNetworkId string `url:"virtual_network_id,omitempty"` - - ResultInfo -} - -// ListInfrastructureTargets returns all applications within an account or zone. -// -// API reference: https://developers.cloudflare.com/api/operations/infra-targets-list -func (api *API) ListInfrastructureTargets(ctx context.Context, rc *ResourceContainer, params InfrastructureTargetListParams) ([]InfrastructureTarget, *ResultInfo, error) { - if rc.Identifier == "" { - return []InfrastructureTarget{}, &ResultInfo{}, ErrMissingAccountID - } - - baseURL := fmt.Sprintf("/%s/%s/infrastructure/targets", rc.Level, rc.Identifier) - - autoPaginate := true - if params.PerPage >= 1 || params.Page >= 1 { - autoPaginate = false - } - - if params.PerPage < 1 { - params.PerPage = 25 - } - - if params.Page < 1 { - params.Page = 1 - } - - var applications []InfrastructureTarget - var r InfrastructureTargetListDetailResponse - - for { - uri := buildURI(baseURL, params) - - res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil) - if err != nil { - return []InfrastructureTarget{}, &ResultInfo{}, fmt.Errorf("%s: %w", errMakeRequestError, err) - } - - err = json.Unmarshal(res, &r) - if err != nil { - return []InfrastructureTarget{}, &ResultInfo{}, fmt.Errorf("%s: %w", errUnmarshalError, err) - } - applications = append(applications, r.Result...) - params.ResultInfo = r.ResultInfo.Next() - if params.ResultInfo.Done() || !autoPaginate { - break - } - } - - return applications, &r.ResultInfo, nil -} - -// CreateInfrastructureTarget creates a new infrastructure target. -// -// API reference: https://developers.cloudflare.com/api/operations/infra-targets-post -func (api *API) CreateInfrastructureTarget(ctx context.Context, rc *ResourceContainer, params CreateInfrastructureTargetParams) (InfrastructureTarget, error) { - if rc.Identifier == "" { - return InfrastructureTarget{}, ErrMissingAccountID - } - - uri := fmt.Sprintf("/%s/%s/infrastructure/targets", rc.Level, rc.Identifier) - - res, err := api.makeRequestContext(ctx, http.MethodPost, uri, params) - if err != nil { - return InfrastructureTarget{}, fmt.Errorf("%s: %w", errMakeRequestError, err) - } - - var targetDetailResponse InfrastructureTargetDetailResponse - err = json.Unmarshal(res, &targetDetailResponse) - if err != nil { - return InfrastructureTarget{}, fmt.Errorf("%s: %w", errUnmarshalError, err) - } - - return targetDetailResponse.Result, nil -} - -// UpdateInfrastructureTarget updates an existing infrastructure target. -// -// API reference: https://developers.cloudflare.com/api/operations/infra-targets-put -func (api *API) UpdateInfrastructureTarget(ctx context.Context, rc *ResourceContainer, params UpdateInfrastructureTargetParams) (InfrastructureTarget, error) { - if rc.Identifier == "" { - return InfrastructureTarget{}, ErrMissingAccountID - } - - if params.ID == "" { - return InfrastructureTarget{}, ErrMissingTargetId - } - - uri := fmt.Sprintf( - "/%s/%s/infrastructure/targets/%s", - rc.Level, - rc.Identifier, - params.ID, - ) - - res, err := api.makeRequestContext(ctx, http.MethodPut, uri, params.ModifyParams) - if err != nil { - return InfrastructureTarget{}, fmt.Errorf("%s: %w", errMakeRequestError, err) - } - - var targetDetailResponse InfrastructureTargetDetailResponse - err = json.Unmarshal(res, &targetDetailResponse) - if err != nil { - return InfrastructureTarget{}, fmt.Errorf("%s: %w", errUnmarshalError, err) - } - - return targetDetailResponse.Result, nil -} - -// GetInfrastructureTarget returns a single infrastructure target based on target ID -// ID for either account or zone. -// -// API reference: https://developers.cloudflare.com/api/operations/infra-targets-get -func (api *API) GetInfrastructureTarget(ctx context.Context, rc *ResourceContainer, targetID string) (InfrastructureTarget, error) { - if rc.Identifier == "" { - return InfrastructureTarget{}, ErrMissingAccountID - } - - if targetID == "" { - return InfrastructureTarget{}, ErrMissingTargetId - } - - uri := fmt.Sprintf( - "/%s/%s/infrastructure/targets/%s", - rc.Level, - rc.Identifier, - targetID, - ) - - res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil) - if err != nil { - return InfrastructureTarget{}, fmt.Errorf("%s: %w", errMakeRequestError, err) - } - - var targetDetailResponse InfrastructureTargetDetailResponse - err = json.Unmarshal(res, &targetDetailResponse) - if err != nil { - return InfrastructureTarget{}, fmt.Errorf("%s: %w", errUnmarshalError, err) - } - - return targetDetailResponse.Result, nil -} - -// DeleteInfrastructureTarget deletes an infrastructure target. -// -// API reference: https://developers.cloudflare.com/api/operations/infra-targets-delete -func (api *API) DeleteInfrastructureTarget(ctx context.Context, rc *ResourceContainer, targetID string) error { - if rc.Identifier == "" { - return ErrMissingAccountID - } - - if targetID == "" { - return ErrMissingTargetId - } - - uri := fmt.Sprintf( - "/%s/%s/infrastructure/targets/%s", - rc.Level, - rc.Identifier, - targetID, - ) - - _, err := api.makeRequestContext(ctx, http.MethodDelete, uri, nil) - if err != nil { - return fmt.Errorf("%s: %w", errMakeRequestError, err) - } - - return nil -} From bab187b471ffad471474640696e3b90d1fa48cd2 Mon Sep 17 00:00:00 2001 From: Sai Dadireddy Date: Sun, 22 Sep 2024 20:38:54 -0700 Subject: [PATCH 10/12] nit: Rename file singular verbiage --- ...structure_access_targets.go => infrastructure_access_target.go | 0 ...access_targets_test.go => infrastructure_access_target_test.go | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename infrastructure_access_targets.go => infrastructure_access_target.go (100%) rename infrastructure_access_targets_test.go => infrastructure_access_target_test.go (100%) diff --git a/infrastructure_access_targets.go b/infrastructure_access_target.go similarity index 100% rename from infrastructure_access_targets.go rename to infrastructure_access_target.go diff --git a/infrastructure_access_targets_test.go b/infrastructure_access_target_test.go similarity index 100% rename from infrastructure_access_targets_test.go rename to infrastructure_access_target_test.go From cbfd80fd83e33b4fe6a187868f018a5b8b57b273 Mon Sep 17 00:00:00 2001 From: Sai Dadireddy Date: Tue, 24 Sep 2024 13:00:01 -0700 Subject: [PATCH 11/12] lint fixes --- infrastructure_access_target_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/infrastructure_access_target_test.go b/infrastructure_access_target_test.go index f00c334c7f0..87c45b79dff 100644 --- a/infrastructure_access_target_test.go +++ b/infrastructure_access_target_test.go @@ -10,7 +10,6 @@ import ( "github.com/stretchr/testify/assert" ) -// randomly generated uuid const testInfrastructureAccessTargetId = "019205b5-97d7-7272-b00e-0ea05e61a124" var ( From c5020a5496d531336c5e59c496cd823844e7bbbb Mon Sep 17 00:00:00 2001 From: Sai Dadireddy Date: Tue, 24 Sep 2024 13:22:31 -0700 Subject: [PATCH 12/12] fix unit tests --- infrastructure_access_target_test.go | 30 ++++++++++++++-------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/infrastructure_access_target_test.go b/infrastructure_access_target_test.go index 87c45b79dff..b80bcda76b0 100644 --- a/infrastructure_access_target_test.go +++ b/infrastructure_access_target_test.go @@ -36,7 +36,7 @@ var ( ID: testInfrastructureAccessTargetId, IP: InfrastructureAccessTargetIPInfo{ IPV4: &InfrastructureAccessTargetIPDetails{ - IPAddr: "250.26.29.250", + IPAddr: "198.51.100.2", VirtualNetworkId: "c77b744e-acc8-428f-9257-6878c046ed55", }, }, @@ -45,7 +45,7 @@ var ( } ) -func TestInfrastructureTarget_Create(t *testing.T) { +func TestInfrastructureAccessTarget_Create(t *testing.T) { setup() defer teardown() @@ -58,12 +58,12 @@ func TestInfrastructureTarget_Create(t *testing.T) { "errors": [], "messages": [], "result": { - "created_at": "2024-08-25T05:00:22Z", + "created_at": "2024-08-25 05:00:22 +0000 UTC", "hostname": "infra-access-target", "id": "019205b5-97d7-7272-b00e-0ea05e61a124", "ip": { "ipv4": { - "ip_addr": "198.51.100.1", + "ip_addr": "187.26.29.249", "virtual_network_id": "c77b744e-acc8-428f-9257-6878c046ed55" }, "ipv6": { @@ -71,7 +71,7 @@ func TestInfrastructureTarget_Create(t *testing.T) { "virtual_network_id": "c77b744e-acc8-428f-9257-6878c046ed55" } }, - "modified_at": "2024-08-25T05:00:22Z" + "modified_at": "2024-08-25 05:00:22 +0000 UTC" } }`) }) @@ -87,7 +87,7 @@ func TestInfrastructureTarget_Create(t *testing.T) { Hostname: "infra-access-target", IP: InfrastructureAccessTargetIPInfo{ IPV4: &InfrastructureAccessTargetIPDetails{ - IPAddr: "198.51.100.1", + IPAddr: "187.26.29.249", VirtualNetworkId: "c77b744e-acc8-428f-9257-6878c046ed55", }, IPV6: &InfrastructureAccessTargetIPDetails{ @@ -98,7 +98,7 @@ func TestInfrastructureTarget_Create(t *testing.T) { }, }) if assert.NoError(t, err) { - assert.Equal(t, expectedInfrastructureAccessTarget, out, "create infrastructure_target structs not equal") + assert.Equal(t, expectedInfrastructureAccessTarget, out, "create infrastructure_access_target structs not equal") } } @@ -116,12 +116,12 @@ func TestInfrastructureTarget_List(t *testing.T) { "messages": [], "result": [ { - "created_at": "2024-08-25T05:00:22Z", + "created_at": "2024-08-25 05:00:22 +0000 UTC", "hostname": "infra-access-target", "id": "019205b5-97d7-7272-b00e-0ea05e61a124", "ip": { "ipv4": { - "ip_addr": "198.51.100.1", + "ip_addr": "187.26.29.249", "virtual_network_id": "c77b744e-acc8-428f-9257-6878c046ed55" }, "ipv6": { @@ -129,7 +129,7 @@ func TestInfrastructureTarget_List(t *testing.T) { "virtual_network_id": "c77b744e-acc8-428f-9257-6878c046ed55" } }, - "modified_at": "2024-08-25T05:00:22Z" + "modified_at": "2024-08-25 05:00:22 +0000 UTC" } ], "result_info": { @@ -167,12 +167,12 @@ func TestInfrastructureTarget_Get(t *testing.T) { "errors": [], "messages": [], "result": { - "created_at": "2024-08-25T05:00:22Z", + "created_at": "2024-08-25 05:00:22 +0000 UTC", "hostname": "infra-access-target", "id": "019205b5-97d7-7272-b00e-0ea05e61a124", "ip": { "ipv4": { - "ip_addr": "198.51.100.1", + "ip_addr": "187.26.29.249", "virtual_network_id": "c77b744e-acc8-428f-9257-6878c046ed55" }, "ipv6": { @@ -180,7 +180,7 @@ func TestInfrastructureTarget_Get(t *testing.T) { "virtual_network_id": "c77b744e-acc8-428f-9257-6878c046ed55" } }, - "modified_at": "2024-08-25T05:00:22Z" + "modified_at": "2024-08-25 05:00:22 +0000 UTC" } }`) }) @@ -215,7 +215,7 @@ func TestInfrastructureTarget_Update(t *testing.T) { "errors": [], "messages": [], "result": { - "created_at": "2024-08-25T05:00:22Z", + "created_at": "2024-08-25 05:00:22 +0000 UTC", "hostname": "infra-access-target-modified", "id": "019205b5-97d7-7272-b00e-0ea05e61a124", "ip": { @@ -224,7 +224,7 @@ func TestInfrastructureTarget_Update(t *testing.T) { "virtual_network_id": "c77b744e-acc8-428f-9257-6878c046ed55" } }, - "modified_at": "2024-08-25T05:00:22Z" + "modified_at": "2024-08-25 05:00:22 +0000 UTC" } }`) })