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

     1  package gtm
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"net/http"
     7  )
     8  
     9  //
    10  // Handle Operations on gtm asmaps
    11  // Based on 1.4 schema
    12  //
    13  
    14  // ASMaps contains operations available on a ASmap resource.
    15  type ASMaps interface {
    16  	// NewAsMap creates a new AsMap object.
    17  	NewAsMap(context.Context, string) *AsMap
    18  	// NewASAssignment instantiates new Assignment struct.
    19  	NewASAssignment(context.Context, *AsMap, int, string) *AsAssignment
    20  	// ListAsMaps retrieves all AsMaps.
    21  	//
    22  	// See: https://techdocs.akamai.com/gtm/reference/get-as-maps
    23  	ListAsMaps(context.Context, string) ([]*AsMap, error)
    24  	// GetAsMap retrieves a AsMap with the given name.
    25  	//
    26  	// See: https://techdocs.akamai.com/gtm/reference/get-as-map
    27  	GetAsMap(context.Context, string, string) (*AsMap, error)
    28  	// CreateAsMap creates the datacenter identified by the receiver argument in the specified domain.
    29  	//
    30  	// See: https://techdocs.akamai.com/gtm/reference/put-as-map
    31  	CreateAsMap(context.Context, *AsMap, string) (*AsMapResponse, error)
    32  	// DeleteAsMap deletes the datacenter identified by the receiver argument from the domain specified.
    33  	//
    34  	// See: https://techdocs.akamai.com/gtm/reference/delete-as-map
    35  	DeleteAsMap(context.Context, *AsMap, string) (*ResponseStatus, error)
    36  	// UpdateAsMap updates the datacenter identified in the receiver argument in the provided domain.
    37  	//
    38  	// See: https://techdocs.akamai.com/gtm/reference/put-as-map
    39  	UpdateAsMap(context.Context, *AsMap, string) (*ResponseStatus, error)
    40  }
    41  
    42  // AsAssignment represents a GTM asmap assignment structure
    43  type AsAssignment struct {
    44  	DatacenterBase
    45  	AsNumbers []int64 `json:"asNumbers"`
    46  }
    47  
    48  // AsMap  represents a GTM AsMap
    49  type AsMap struct {
    50  	DefaultDatacenter *DatacenterBase `json:"defaultDatacenter"`
    51  	Assignments       []*AsAssignment `json:"assignments,omitempty"`
    52  	Name              string          `json:"name"`
    53  	Links             []*Link         `json:"links,omitempty"`
    54  }
    55  
    56  // AsMapList represents the returned GTM AsMap List body
    57  type AsMapList struct {
    58  	AsMapItems []*AsMap `json:"items"`
    59  }
    60  
    61  // Validate validates AsMap
    62  func (asm *AsMap) Validate() error {
    63  
    64  	if len(asm.Name) < 1 {
    65  		return fmt.Errorf("AsMap is missing Name")
    66  	}
    67  	if asm.DefaultDatacenter == nil {
    68  		return fmt.Errorf("AsMap is missing DefaultDatacenter")
    69  	}
    70  
    71  	return nil
    72  }
    73  
    74  func (p *gtm) NewAsMap(ctx context.Context, name string) *AsMap {
    75  
    76  	logger := p.Log(ctx)
    77  	logger.Debug("NewAsMap")
    78  
    79  	asmap := &AsMap{Name: name}
    80  	return asmap
    81  }
    82  
    83  func (p *gtm) ListAsMaps(ctx context.Context, domainName string) ([]*AsMap, error) {
    84  
    85  	logger := p.Log(ctx)
    86  	logger.Debug("ListAsMaps")
    87  
    88  	var aslist AsMapList
    89  	getURL := fmt.Sprintf("/config-gtm/v1/domains/%s/as-maps", domainName)
    90  	req, err := http.NewRequestWithContext(ctx, http.MethodGet, getURL, nil)
    91  	if err != nil {
    92  		return nil, fmt.Errorf("failed to create ListAsMaps request: %w", err)
    93  	}
    94  	setVersionHeader(req, schemaVersion)
    95  	resp, err := p.Exec(req, &aslist)
    96  	if err != nil {
    97  		return nil, fmt.Errorf("ListAsMaps request failed: %w", err)
    98  	}
    99  
   100  	if resp.StatusCode != http.StatusOK {
   101  		return nil, p.Error(resp)
   102  	}
   103  
   104  	return aslist.AsMapItems, nil
   105  }
   106  
   107  func (p *gtm) GetAsMap(ctx context.Context, name, domainName string) (*AsMap, error) {
   108  
   109  	logger := p.Log(ctx)
   110  	logger.Debug("GetAsMap")
   111  
   112  	var as AsMap
   113  	getURL := fmt.Sprintf("/config-gtm/v1/domains/%s/as-maps/%s", domainName, name)
   114  	req, err := http.NewRequestWithContext(ctx, http.MethodGet, getURL, nil)
   115  	if err != nil {
   116  		return nil, fmt.Errorf("failed to create GetAsMap request: %w", err)
   117  	}
   118  	setVersionHeader(req, schemaVersion)
   119  	resp, err := p.Exec(req, &as)
   120  	if err != nil {
   121  		return nil, fmt.Errorf("GetAsMap request failed: %w", err)
   122  	}
   123  
   124  	if resp.StatusCode != http.StatusOK {
   125  		return nil, p.Error(resp)
   126  	}
   127  
   128  	return &as, nil
   129  }
   130  
   131  func (p *gtm) NewASAssignment(ctx context.Context, _ *AsMap, dcID int, nickname string) *AsAssignment {
   132  
   133  	logger := p.Log(ctx)
   134  	logger.Debug("NewAssignment")
   135  
   136  	asAssign := &AsAssignment{}
   137  	asAssign.DatacenterId = dcID
   138  	asAssign.Nickname = nickname
   139  
   140  	return asAssign
   141  }
   142  
   143  func (p *gtm) CreateAsMap(ctx context.Context, as *AsMap, domainName string) (*AsMapResponse, error) {
   144  
   145  	logger := p.Log(ctx)
   146  	logger.Debug("CreateAsMap")
   147  
   148  	// Use common code. Any specific validation needed?
   149  	return as.save(ctx, p, domainName)
   150  }
   151  
   152  func (p *gtm) UpdateAsMap(ctx context.Context, as *AsMap, domainName string) (*ResponseStatus, error) {
   153  
   154  	logger := p.Log(ctx)
   155  	logger.Debug("UpdateAsMap")
   156  
   157  	// common code
   158  	stat, err := as.save(ctx, p, domainName)
   159  	if err != nil {
   160  		return nil, err
   161  	}
   162  	return stat.Status, err
   163  }
   164  
   165  // save AsMap in given domain. Common path for Create and Update.
   166  func (asm *AsMap) save(ctx context.Context, p *gtm, domainName string) (*AsMapResponse, error) {
   167  
   168  	if err := asm.Validate(); err != nil {
   169  		return nil, fmt.Errorf("AsMap validation failed. %w", err)
   170  	}
   171  
   172  	putURL := fmt.Sprintf("/config-gtm/v1/domains/%s/as-maps/%s", domainName, asm.Name)
   173  	req, err := http.NewRequestWithContext(ctx, http.MethodPut, putURL, nil)
   174  	if err != nil {
   175  		return nil, fmt.Errorf("failed to create AsMap request: %w", err)
   176  	}
   177  
   178  	var mapresp AsMapResponse
   179  	setVersionHeader(req, schemaVersion)
   180  	resp, err := p.Exec(req, &mapresp, asm)
   181  	if err != nil {
   182  		return nil, fmt.Errorf("AsMap request failed: %w", err)
   183  	}
   184  
   185  	if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
   186  		return nil, p.Error(resp)
   187  	}
   188  
   189  	return &mapresp, nil
   190  }
   191  
   192  func (p *gtm) DeleteAsMap(ctx context.Context, as *AsMap, domainName string) (*ResponseStatus, error) {
   193  
   194  	logger := p.Log(ctx)
   195  	logger.Debug("DeleteAsMap")
   196  
   197  	if err := as.Validate(); err != nil {
   198  		return nil, fmt.Errorf("Resource validation failed. %w", err)
   199  	}
   200  
   201  	delURL := fmt.Sprintf("/config-gtm/v1/domains/%s/as-maps/%s", domainName, as.Name)
   202  	req, err := http.NewRequestWithContext(ctx, http.MethodDelete, delURL, nil)
   203  	if err != nil {
   204  		return nil, fmt.Errorf("failed to create Delete request: %w", err)
   205  	}
   206  
   207  	var mapresp ResponseBody
   208  	setVersionHeader(req, schemaVersion)
   209  	resp, err := p.Exec(req, &mapresp)
   210  	if err != nil {
   211  		return nil, fmt.Errorf("AsMap request failed: %w", err)
   212  	}
   213  
   214  	if resp.StatusCode != http.StatusOK {
   215  		return nil, p.Error(resp)
   216  	}
   217  
   218  	return mapresp.Status, nil
   219  }