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

     1  package gtm
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"net/http"
     7  )
     8  
     9  // ASMaps contains operations available on a ASmap resource.
    10  type (
    11  	ASMaps interface {
    12  		// ListASMaps retrieves all AsMaps.
    13  		//
    14  		// See: https://techdocs.akamai.com/gtm/reference/get-as-maps
    15  		ListASMaps(context.Context, string) ([]*ASMap, error)
    16  		// GetASMap retrieves a AsMap with the given name.
    17  		//
    18  		// See: https://techdocs.akamai.com/gtm/reference/get-as-map
    19  		GetASMap(context.Context, string, string) (*ASMap, error)
    20  		// CreateASMap creates the datacenter identified by the receiver argument in the specified domain.
    21  		//
    22  		// See: https://techdocs.akamai.com/gtm/reference/put-as-map
    23  		CreateASMap(context.Context, *ASMap, string) (*ASMapResponse, error)
    24  		// DeleteASMap deletes the datacenter identified by the receiver argument from the domain specified.
    25  		//
    26  		// See: https://techdocs.akamai.com/gtm/reference/delete-as-map
    27  		DeleteASMap(context.Context, *ASMap, string) (*ResponseStatus, error)
    28  		// UpdateASMap updates the datacenter identified in the receiver argument in the provided domain.
    29  		//
    30  		// See: https://techdocs.akamai.com/gtm/reference/put-as-map
    31  		UpdateASMap(context.Context, *ASMap, string) (*ResponseStatus, error)
    32  	}
    33  	// ASAssignment represents a GTM as map assignment structure
    34  	ASAssignment struct {
    35  		DatacenterBase
    36  		ASNumbers []int64 `json:"asNumbers"`
    37  	}
    38  
    39  	// ASMap  represents a GTM ASMap
    40  	ASMap struct {
    41  		DefaultDatacenter *DatacenterBase `json:"defaultDatacenter"`
    42  		Assignments       []*ASAssignment `json:"assignments,omitempty"`
    43  		Name              string          `json:"name"`
    44  		Links             []*Link         `json:"links,omitempty"`
    45  	}
    46  
    47  	// ASMapList represents the returned GTM ASMap List body
    48  	ASMapList struct {
    49  		ASMapItems []*ASMap `json:"items"`
    50  	}
    51  )
    52  
    53  // Validate validates ASMap
    54  func (a *ASMap) Validate() error {
    55  	if len(a.Name) < 1 {
    56  		return fmt.Errorf("ASMap is missing Name")
    57  	}
    58  	if a.DefaultDatacenter == nil {
    59  		return fmt.Errorf("ASMap is missing DefaultDatacenter")
    60  	}
    61  
    62  	return nil
    63  }
    64  
    65  func (g *gtm) ListASMaps(ctx context.Context, domainName string) ([]*ASMap, error) {
    66  	logger := g.Log(ctx)
    67  	logger.Debug("ListASMaps")
    68  
    69  	getURL := fmt.Sprintf("/config-gtm/v1/domains/%s/as-maps", domainName)
    70  	req, err := http.NewRequestWithContext(ctx, http.MethodGet, getURL, nil)
    71  	if err != nil {
    72  		return nil, fmt.Errorf("failed to create ListASMaps request: %w", err)
    73  	}
    74  	setVersionHeader(req, schemaVersion)
    75  
    76  	var result ASMapList
    77  	resp, err := g.Exec(req, &result)
    78  	if err != nil {
    79  		return nil, fmt.Errorf("ListASMaps request failed: %w", err)
    80  	}
    81  
    82  	if resp.StatusCode != http.StatusOK {
    83  		return nil, g.Error(resp)
    84  	}
    85  
    86  	return result.ASMapItems, nil
    87  }
    88  
    89  func (g *gtm) GetASMap(ctx context.Context, asMapName, domainName string) (*ASMap, error) {
    90  	logger := g.Log(ctx)
    91  	logger.Debug("GetASMap")
    92  
    93  	getURL := fmt.Sprintf("/config-gtm/v1/domains/%s/as-maps/%s", domainName, asMapName)
    94  	req, err := http.NewRequestWithContext(ctx, http.MethodGet, getURL, nil)
    95  	if err != nil {
    96  		return nil, fmt.Errorf("failed to create GetASMap request: %w", err)
    97  	}
    98  	setVersionHeader(req, schemaVersion)
    99  
   100  	var result ASMap
   101  	resp, err := g.Exec(req, &result)
   102  	if err != nil {
   103  		return nil, fmt.Errorf("GetASMap request failed: %w", err)
   104  	}
   105  
   106  	if resp.StatusCode != http.StatusOK {
   107  		return nil, g.Error(resp)
   108  	}
   109  
   110  	return &result, nil
   111  }
   112  
   113  func (g *gtm) CreateASMap(ctx context.Context, asMap *ASMap, domainName string) (*ASMapResponse, error) {
   114  	logger := g.Log(ctx)
   115  	logger.Debug("CreateASMap")
   116  
   117  	return asMap.save(ctx, g, domainName)
   118  }
   119  
   120  func (g *gtm) UpdateASMap(ctx context.Context, asMap *ASMap, domainName string) (*ResponseStatus, error) {
   121  	logger := g.Log(ctx)
   122  	logger.Debug("UpdateASMap")
   123  
   124  	stat, err := asMap.save(ctx, g, domainName)
   125  	if err != nil {
   126  		return nil, err
   127  	}
   128  	return stat.Status, err
   129  }
   130  
   131  // save AsMap in given domain. Common path for Create and Update.
   132  func (a *ASMap) save(ctx context.Context, g *gtm, domainName string) (*ASMapResponse, error) {
   133  	if err := a.Validate(); err != nil {
   134  		return nil, fmt.Errorf("ASMap validation failed. %w", err)
   135  	}
   136  
   137  	putURL := fmt.Sprintf("/config-gtm/v1/domains/%s/as-maps/%s", domainName, a.Name)
   138  	req, err := http.NewRequestWithContext(ctx, http.MethodPut, putURL, nil)
   139  	if err != nil {
   140  		return nil, fmt.Errorf("failed to create ASMap request: %w", err)
   141  	}
   142  	setVersionHeader(req, schemaVersion)
   143  
   144  	var result ASMapResponse
   145  	resp, err := g.Exec(req, &result, a)
   146  	if err != nil {
   147  		return nil, fmt.Errorf("ASMap request failed: %w", err)
   148  	}
   149  
   150  	if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
   151  		return nil, g.Error(resp)
   152  	}
   153  
   154  	return &result, nil
   155  }
   156  
   157  func (g *gtm) DeleteASMap(ctx context.Context, asMap *ASMap, domainName string) (*ResponseStatus, error) {
   158  	logger := g.Log(ctx)
   159  	logger.Debug("DeleteASMap")
   160  
   161  	if err := asMap.Validate(); err != nil {
   162  		return nil, fmt.Errorf("resource validation failed: %w", err)
   163  	}
   164  
   165  	delURL := fmt.Sprintf("/config-gtm/v1/domains/%s/as-maps/%s", domainName, asMap.Name)
   166  	req, err := http.NewRequestWithContext(ctx, http.MethodDelete, delURL, nil)
   167  	if err != nil {
   168  		return nil, fmt.Errorf("failed to create Delete request: %w", err)
   169  	}
   170  	setVersionHeader(req, schemaVersion)
   171  
   172  	var result ResponseBody
   173  	resp, err := g.Exec(req, &result)
   174  	if err != nil {
   175  		return nil, fmt.Errorf("ASMap request failed: %w", err)
   176  	}
   177  
   178  	if resp.StatusCode != http.StatusOK {
   179  		return nil, g.Error(resp)
   180  	}
   181  
   182  	return result.Status, nil
   183  }