github.com/akamai/AkamaiOPEN-edgegrid-golang/v8@v8.1.0/pkg/iam/support.go (about)

     1  package iam
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"fmt"
     7  	"net/http"
     8  
     9  	validation "github.com/go-ozzo/ozzo-validation/v4"
    10  )
    11  
    12  type (
    13  	// Support is a list of IAM supported objects API interfaces
    14  	Support interface {
    15  		// ListProducts lists products a user can subscribe to and receive notifications for on the account
    16  		//
    17  		// See: https://techdocs.akamai.com/iam-user-admin/reference/get-common-notification-products
    18  		ListProducts(context.Context) ([]string, error)
    19  
    20  		// ListStates lists U.S. states or Canadian provinces
    21  		//
    22  		// See: https://techdocs.akamai.com/iam-user-admin/reference/get-common-states
    23  		ListStates(context.Context, ListStatesRequest) ([]string, error)
    24  
    25  		// ListTimeoutPolicies lists all the possible session timeout policies
    26  		//
    27  		// See: https://techdocs.akamai.com/iam-user-admin/reference/get-common-timeout-policies
    28  		ListTimeoutPolicies(context.Context) ([]TimeoutPolicy, error)
    29  
    30  		// SupportedContactTypes lists supported contact types
    31  		//
    32  		// See: https://techdocs.akamai.com/iam-user-admin/reference/get-common-contact-types
    33  		SupportedContactTypes(context.Context) ([]string, error)
    34  
    35  		// SupportedCountries lists supported countries
    36  		//
    37  		// See: https://techdocs.akamai.com/iam-user-admin/reference/get-common-countries
    38  		SupportedCountries(context.Context) ([]string, error)
    39  
    40  		// SupportedLanguages lists supported languages
    41  		//
    42  		// See: https://techdocs.akamai.com/iam-user-admin/reference/get-common-languages
    43  		SupportedLanguages(context.Context) ([]string, error)
    44  
    45  		// SupportedTimezones lists supported timezones
    46  		//
    47  		// See: https://techdocs.akamai.com/iam-user-admin/reference/get-common-timezones
    48  		SupportedTimezones(context.Context) ([]Timezone, error)
    49  	}
    50  
    51  	// TimeoutPolicy encapsulates the response of the list timeout policies endpoint
    52  	TimeoutPolicy struct {
    53  		Name  string `json:"name"`
    54  		Value int64  `json:"value"`
    55  	}
    56  
    57  	// ListStatesRequest contains the country request parameter for the list states endpoint
    58  	ListStatesRequest struct {
    59  		Country string
    60  	}
    61  
    62  	// Timezone contains the response of the list supported timezones endpoint
    63  	Timezone struct {
    64  		Description string `json:"description"`
    65  		Offset      string `json:"offset"`
    66  		Posix       string `json:"posix"`
    67  		Timezone    string `json:"timezone"`
    68  	}
    69  )
    70  
    71  var (
    72  	// ErrListProducts is returned when ListProducts fails
    73  	ErrListProducts = errors.New("list products")
    74  
    75  	// ErrListStates is returned when ListStates fails
    76  	ErrListStates = errors.New("list states")
    77  
    78  	// ErrListTimeoutPolicies is returned when ListTimeoutPolicies fails
    79  	ErrListTimeoutPolicies = errors.New("list timeout policies")
    80  
    81  	// ErrSupportedContactTypes is returned when SupportedContactTypes fails
    82  	ErrSupportedContactTypes = errors.New("supported contact types")
    83  
    84  	// ErrSupportedCountries is returned when SupportedCountries fails
    85  	ErrSupportedCountries = errors.New("supported countries")
    86  
    87  	// ErrSupportedLanguages is returned when SupportedLanguages fails
    88  	ErrSupportedLanguages = errors.New("supported languages")
    89  
    90  	// ErrSupportedTimezones is returned when SupportedTimezones fails
    91  	ErrSupportedTimezones = errors.New("supported timezones")
    92  )
    93  
    94  // Validate validates ListStatesRequest
    95  func (r ListStatesRequest) Validate() error {
    96  	return validation.Errors{
    97  		"country": validation.Validate(r.Country, validation.Required),
    98  	}.Filter()
    99  }
   100  
   101  func (i *iam) ListProducts(ctx context.Context) ([]string, error) {
   102  	logger := i.Log(ctx)
   103  	logger.Debug("ListProducts")
   104  
   105  	getURL := "/identity-management/v2/user-admin/common/notification-products"
   106  
   107  	req, err := http.NewRequestWithContext(ctx, http.MethodGet, getURL, nil)
   108  	if err != nil {
   109  		return nil, fmt.Errorf("%w: failed to create request: %s", ErrListProducts, err)
   110  	}
   111  
   112  	var rval []string
   113  	resp, err := i.Exec(req, &rval)
   114  	if err != nil {
   115  		return nil, fmt.Errorf("%w: request failed: %s", ErrListProducts, err)
   116  	}
   117  
   118  	if resp.StatusCode != http.StatusOK {
   119  		return nil, fmt.Errorf("%s: %w", ErrListProducts, i.Error(resp))
   120  	}
   121  
   122  	return rval, nil
   123  }
   124  
   125  func (i *iam) ListStates(ctx context.Context, params ListStatesRequest) ([]string, error) {
   126  	logger := i.Log(ctx)
   127  	logger.Debug("ListStates")
   128  
   129  	if err := params.Validate(); err != nil {
   130  		return nil, fmt.Errorf("%s: %w:\n%s", ErrListStates, ErrStructValidation, err)
   131  	}
   132  
   133  	getURL := fmt.Sprintf("/identity-management/v2/user-admin/common/countries/%s/states", params.Country)
   134  
   135  	req, err := http.NewRequestWithContext(ctx, http.MethodGet, getURL, nil)
   136  	if err != nil {
   137  		return nil, fmt.Errorf("%w: failed to create request: %s", ErrListStates, err)
   138  	}
   139  
   140  	var rval []string
   141  	resp, err := i.Exec(req, &rval)
   142  	if err != nil {
   143  		return nil, fmt.Errorf("%w: request failed: %s", ErrListStates, err)
   144  	}
   145  
   146  	if resp.StatusCode != http.StatusOK {
   147  		return nil, fmt.Errorf("%s: %w", ErrListStates, i.Error(resp))
   148  	}
   149  
   150  	return rval, nil
   151  }
   152  
   153  func (i *iam) ListTimeoutPolicies(ctx context.Context) ([]TimeoutPolicy, error) {
   154  	logger := i.Log(ctx)
   155  	logger.Debug("ListTimeoutPolicies")
   156  
   157  	getURL := "/identity-management/v2/user-admin/common/timeout-policies"
   158  
   159  	req, err := http.NewRequestWithContext(ctx, http.MethodGet, getURL, nil)
   160  	if err != nil {
   161  		return nil, fmt.Errorf("%w: failed to create request: %s", ErrListTimeoutPolicies, err)
   162  	}
   163  
   164  	var rval []TimeoutPolicy
   165  	resp, err := i.Exec(req, &rval)
   166  	if err != nil {
   167  		return nil, fmt.Errorf("%w: request failed: %s", ErrListTimeoutPolicies, err)
   168  	}
   169  
   170  	if resp.StatusCode != http.StatusOK {
   171  		return nil, fmt.Errorf("%s: %w", ErrListTimeoutPolicies, i.Error(resp))
   172  	}
   173  
   174  	return rval, nil
   175  }
   176  
   177  func (i *iam) SupportedContactTypes(ctx context.Context) ([]string, error) {
   178  	logger := i.Log(ctx)
   179  	logger.Debug("SupportedContactTypes")
   180  
   181  	getURL := "/identity-management/v2/user-admin/common/contact-types"
   182  
   183  	req, err := http.NewRequestWithContext(ctx, http.MethodGet, getURL, nil)
   184  	if err != nil {
   185  		return nil, fmt.Errorf("%w: failed to create request: %s", ErrSupportedContactTypes, err)
   186  	}
   187  
   188  	var rval []string
   189  	resp, err := i.Exec(req, &rval)
   190  	if err != nil {
   191  		return nil, fmt.Errorf("%w: request failed: %s", ErrSupportedContactTypes, err)
   192  	}
   193  
   194  	if resp.StatusCode != http.StatusOK {
   195  		return nil, fmt.Errorf("%s: %w", ErrSupportedContactTypes, i.Error(resp))
   196  	}
   197  
   198  	return rval, nil
   199  }
   200  
   201  func (i *iam) SupportedCountries(ctx context.Context) ([]string, error) {
   202  	logger := i.Log(ctx)
   203  	logger.Debug("SupportedCountries")
   204  
   205  	getURL := "/identity-management/v2/user-admin/common/countries"
   206  
   207  	req, err := http.NewRequestWithContext(ctx, http.MethodGet, getURL, nil)
   208  	if err != nil {
   209  		return nil, fmt.Errorf("%w: failed to create request: %s", ErrSupportedCountries, err)
   210  	}
   211  
   212  	var rval []string
   213  	resp, err := i.Exec(req, &rval)
   214  	if err != nil {
   215  		return nil, fmt.Errorf("%w: request failed: %s", ErrSupportedCountries, err)
   216  	}
   217  
   218  	if resp.StatusCode != http.StatusOK {
   219  		return nil, fmt.Errorf("%s: %w", ErrSupportedCountries, i.Error(resp))
   220  	}
   221  
   222  	return rval, nil
   223  }
   224  
   225  func (i *iam) SupportedLanguages(ctx context.Context) ([]string, error) {
   226  	logger := i.Log(ctx)
   227  	logger.Debug("SupportedLanguages")
   228  
   229  	getURL := "/identity-management/v2/user-admin/common/supported-languages"
   230  
   231  	req, err := http.NewRequestWithContext(ctx, http.MethodGet, getURL, nil)
   232  	if err != nil {
   233  		return nil, fmt.Errorf("%w: failed to create request: %s", ErrSupportedLanguages, err)
   234  	}
   235  
   236  	var rval []string
   237  	resp, err := i.Exec(req, &rval)
   238  	if err != nil {
   239  		return nil, fmt.Errorf("%w: request failed: %s", ErrSupportedLanguages, err)
   240  	}
   241  
   242  	if resp.StatusCode != http.StatusOK {
   243  		return nil, fmt.Errorf("%s: %w", ErrSupportedLanguages, i.Error(resp))
   244  	}
   245  
   246  	return rval, nil
   247  }
   248  
   249  func (i *iam) SupportedTimezones(ctx context.Context) ([]Timezone, error) {
   250  	logger := i.Log(ctx)
   251  	logger.Debug("SupportedTimezones")
   252  
   253  	getURL := "/identity-management/v2/user-admin/common/timezones"
   254  
   255  	req, err := http.NewRequestWithContext(ctx, http.MethodGet, getURL, nil)
   256  	if err != nil {
   257  		return nil, fmt.Errorf("%w: failed to create request: %s", ErrSupportedTimezones, err)
   258  	}
   259  
   260  	var rval []Timezone
   261  	resp, err := i.Exec(req, &rval)
   262  	if err != nil {
   263  		return nil, fmt.Errorf("%w: request failed: %s", ErrSupportedTimezones, err)
   264  	}
   265  
   266  	if resp.StatusCode != http.StatusOK {
   267  		return nil, fmt.Errorf("%s: %w", ErrSupportedTimezones, i.Error(resp))
   268  	}
   269  
   270  	return rval, nil
   271  }