Skip to content

Commit

Permalink
Merge pull request #693 from rexscaria/rex/GATE-1703-teams-api
Browse files Browse the repository at this point in the history
GATE-1703 team rules + accounts + config api
  • Loading branch information
jacobbednarz authored Aug 18, 2021
2 parents f1ea180 + 44b952d commit c26ab13
Show file tree
Hide file tree
Showing 4 changed files with 917 additions and 0 deletions.
128 changes: 128 additions & 0 deletions teams_accounts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package cloudflare

import (
"context"
"encoding/json"
"fmt"
"net/http"
"time"

"github.com/pkg/errors"
)

type TeamsAccount struct {
GatewayTag string `json:"gateway_tag"` // Internal teams ID
ProviderName string `json:"provider_name"` // Auth provider
ID string `json:"id"` // cloudflare account ID
}

// TeamsAccountResponse is the API response, containing information on teams
// account.
type TeamsAccountResponse struct {
Response
Result TeamsAccount `json:"result"`
}

// TeamsConfigResponse is the API response, containing information on teams
// account config.
type TeamsConfigResponse struct {
Response
Result TeamsConfiguration `json:"result"`
}

// TeamsConfiguration data model.
type TeamsConfiguration struct {
Settings TeamsAccountSettings `json:"settings"`
CreatedAt time.Time `json:"created_at,omitempty"`
UpdatedAt time.Time `json:"updated_at,omitempty"`
}

type TeamsAccountSettings struct {
Antivirus *TeamsAntivirus `json:"antivirus,omitempty"`
TLSDecrypt *TeamsTLSDecrypt `json:"tls_decrypt,omitempty"`
ActivityLog *TeamsActivityLog `json:"activity_log,omitempty"`
BlockPage *TeamsBlockPage `json:"block_page,omitempty"`
}

type TeamsAntivirus struct {
EnabledDownloadPhase bool `json:"enabled_download_phase,omitempty"`
EnabledUploadPhase bool `json:"enabled_upload_phase,omitempty"`
FailClosed bool `json:"fail_closed,omitempty"`
}

type TeamsTLSDecrypt struct {
Enabled bool `json:"enabled,omitempty"`
}

type TeamsActivityLog struct {
Enabled bool `json:"enabled,omitempty"`
}

type TeamsBlockPage struct {
Enabled bool `json:"enabled,omitempty"`
FooterText string `json:"footer_text,omitempty"`
HeaderText string `json:"header_text,omitempty"`
LogoPath string `json:"logo_path,omitempty"`
BackgroundColor string `json:"background_color,omitempty"`
Name string `json:"name,omitempty"`
}

// TeamsAccount returns teams account information with internal and external ID.
//
// API reference: TBA
func (api *API) TeamsAccount(ctx context.Context, accountID string) (TeamsAccount, error) {
uri := fmt.Sprintf("/accounts/%s/gateway", accountID)

res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return TeamsAccount{}, err
}

var teamsAccountResponse TeamsAccountResponse
err = json.Unmarshal(res, &teamsAccountResponse)
if err != nil {
return TeamsAccount{}, errors.Wrap(err, errUnmarshalError)
}

return teamsAccountResponse.Result, nil
}

// TeamsAccountConfiguration returns teams account configuration.
//
// API reference: TBA
func (api *API) TeamsAccountConfiguration(ctx context.Context, accountID string) (TeamsConfiguration, error) {
uri := fmt.Sprintf("/accounts/%s/gateway/configuration", accountID)

res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return TeamsConfiguration{}, err
}

var teamsConfigResponse TeamsConfigResponse
err = json.Unmarshal(res, &teamsConfigResponse)
if err != nil {
return TeamsConfiguration{}, errors.Wrap(err, errUnmarshalError)
}

return teamsConfigResponse.Result, nil
}

// TeamsAccountUpdateConfiguration updates a teams account configuration.
//
// API reference: TBA
func (api *API) TeamsAccountUpdateConfiguration(ctx context.Context, accountID string, config TeamsConfiguration) (TeamsConfiguration, error) {
uri := fmt.Sprintf("/accounts/%s/gateway/configuration", accountID)

res, err := api.makeRequestContext(ctx, http.MethodPut, uri, config)
if err != nil {
return TeamsConfiguration{}, err
}

var teamsConfigResponse TeamsConfigResponse
err = json.Unmarshal(res, &teamsConfigResponse)
if err != nil {
return TeamsConfiguration{}, errors.Wrap(err, errUnmarshalError)
}

return teamsConfigResponse.Result, nil
}
129 changes: 129 additions & 0 deletions teams_accounts_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package cloudflare

import (
"context"
"fmt"
"net/http"
"testing"

"github.com/stretchr/testify/assert"
)

func TestTeamsAccount(t *testing.T) {
setup()
defer teardown()

handler := 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.Fprintf(w, `{
"success": true,
"errors": [],
"messages": [],
"result": {
"id": "%s",
"provider_name": "cf",
"gateway_tag": "1234"
}
}
`, testAccountID)
}

mux.HandleFunc("/accounts/"+testAccountID+"/gateway", handler)

actual, err := client.TeamsAccount(context.Background(), testAccountID)

if assert.NoError(t, err) {
assert.Equal(t, actual.ProviderName, "cf")
assert.Equal(t, actual.GatewayTag, "1234")
assert.Equal(t, actual.ID, testAccountID)
}
}

func TestTeamsAccountConfiguration(t *testing.T) {
setup()
defer teardown()

handler := 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.Fprintf(w, `{
"success": true,
"errors": [],
"messages": [],
"result": {
"settings": {
"antivirus": {
"enabled_download_phase": true
},
"tls_decrypt": {
"enabled": true
},
"activity_log": {
"enabled": true
}
}
}
}
`)
}

mux.HandleFunc("/accounts/"+testAccountID+"/gateway/configuration", handler)

actual, err := client.TeamsAccountConfiguration(context.Background(), testAccountID)

if assert.NoError(t, err) {
assert.Equal(t, actual.Settings, TeamsAccountSettings{
Antivirus: &TeamsAntivirus{EnabledDownloadPhase: true},
ActivityLog: &TeamsActivityLog{Enabled: true},
TLSDecrypt: &TeamsTLSDecrypt{Enabled: true},
})
}
}

func TestTeamsAccountUpdateConfiguration(t *testing.T) {
setup()
defer teardown()

handler := 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.Fprintf(w, `{
"success": true,
"errors": [],
"messages": [],
"result": {
"settings": {
"antivirus": {
"enabled_download_phase": false
},
"tls_decrypt": {
"enabled": true
},
"activity_log": {
"enabled": true
}
}
}
}
`)
}

settings := TeamsAccountSettings{
Antivirus: &TeamsAntivirus{EnabledDownloadPhase: false},
ActivityLog: &TeamsActivityLog{Enabled: true},
TLSDecrypt: &TeamsTLSDecrypt{Enabled: true},
}

mux.HandleFunc("/accounts/"+testAccountID+"/gateway/configuration", handler)

configuration := TeamsConfiguration{
Settings: settings,
}
actual, err := client.TeamsAccountUpdateConfiguration(context.Background(), testAccountID, configuration)

if assert.NoError(t, err) {

assert.Equal(t, actual, configuration)
}
}
Loading

0 comments on commit c26ab13

Please sign in to comment.