github.com/akamai/AkamaiOPEN-edgegrid-golang@v1.2.2/reportsgtm-v1/property.go (about)

     1  package reportsgtm
     2  
     3  import (
     4  	"github.com/akamai/AkamaiOPEN-edgegrid-golang/client-v1"
     5  	"github.com/akamai/AkamaiOPEN-edgegrid-golang/configgtm-v1_3"
     6  
     7  	"fmt"
     8  )
     9  
    10  //
    11  // Support gtm reports thru Edgegrid
    12  // Based on 1.0 Schema
    13  //
    14  
    15  // Property Traffic Report Structs
    16  type PropertyTMeta struct {
    17  	Uri      string `json:uri"`
    18  	Domain   string `json:"domain"`
    19  	Interval string `json:"interval,omitempty"`
    20  	Property string `json:"property"`
    21  	Start    string `json:"start"`
    22  	End      string `json:"end"`
    23  }
    24  
    25  type PropertyDRow struct {
    26  	Nickname          string `json:"nickname"`
    27  	DatacenterId      int    `json:"datacenterId"`
    28  	TrafficTargetName string `json:"trafficTargetName"`
    29  	Requests          int64  `json:"requests"`
    30  	Status            string `json:"status"`
    31  }
    32  
    33  type PropertyTData struct {
    34  	Timestamp   string          `json:"timestamp"`
    35  	Datacenters []*PropertyDRow `json:"datacenters"`
    36  }
    37  
    38  // The Property Traffic Response structure returned by the Reports API
    39  type PropertyTrafficResponse struct {
    40  	Metadata    *PropertyTMeta    `json:"metadata"`
    41  	DataRows    []*PropertyTData  `json:"dataRows"`
    42  	DataSummary interface{}       `json:"dataSummary"`
    43  	Links       []*configgtm.Link `json:"links"`
    44  }
    45  
    46  //
    47  // IP Status By Property Structs
    48  //
    49  
    50  // IP Availability Status Response structure returned by the Reports API.
    51  type IPStatusPerProperty struct {
    52  	Metadata    *IpStatPerPropMeta   `json:"metadata"`
    53  	DataRows    []*IpStatPerPropData `json:"dataRows"`
    54  	DataSummary interface{}          `json:"dataSummary"`
    55  	Links       []*configgtm.Link    `json:"links"`
    56  }
    57  
    58  type IpStatPerPropMeta struct {
    59  	Uri          string `json:uri"`
    60  	Domain       string `json:"domain"`
    61  	Property     string `json:"property"`
    62  	Start        string `json:"start"`
    63  	End          string `json:"end"`
    64  	MostRecent   bool   `json:"mostRecent"`
    65  	Ip           string `json:"ip"`
    66  	DatacenterId int    `json:"datacenterId"`
    67  }
    68  
    69  type IpStatPerPropData struct {
    70  	Timestamp   string               `json:"timestamp"`
    71  	CutOff      float64              `json:"cutOff"`
    72  	Datacenters []*IpStatPerPropDRow `json:"datacenters"`
    73  }
    74  
    75  type IpStatPerPropDRow struct {
    76  	Nickname          string      `json:"nickname"`
    77  	DatacenterId      int         `json:"datacenterId"`
    78  	TrafficTargetName string      `json:"trafficTargetName"`
    79  	IPs               []*IpStatIp `json:"IPs"`
    80  }
    81  
    82  type IpStatIp struct {
    83  	Ip        string  `json:"ip"`
    84  	HandedOut bool    `json:"handedOut"`
    85  	Score     float32 `json:"score"`
    86  	Alive     bool    `json:"alive"`
    87  }
    88  
    89  // GetIpStatusPerProperty retrieves current IP Availability Status for specified property in the given domainname.
    90  func GetIpStatusPerProperty(domainName string, propertyName string, optArgs map[string]string) (*IPStatusPerProperty, error) {
    91  	stat := &IPStatusPerProperty{}
    92  	hostURL := fmt.Sprintf("/gtm-api/v1/reports/ip-availability/domains/%s/properties/%s", domainName, propertyName)
    93  
    94  	req, err := client.NewRequest(
    95  		Config,
    96  		"GET",
    97  		hostURL,
    98  		nil,
    99  	)
   100  	if err != nil {
   101  		return nil, err
   102  	}
   103  
   104  	// Look for and process optional query params
   105  	q := req.URL.Query()
   106  	for k, v := range optArgs {
   107  		switch k {
   108  		case "start":
   109  			fallthrough
   110  		case "end":
   111  			fallthrough
   112  		case "ip":
   113  			fallthrough
   114  		case "mostRecent":
   115  			fallthrough
   116  		case "datacenterId":
   117  			q.Add(k, v)
   118  		}
   119  	}
   120  	if optArgs != nil {
   121  		req.URL.RawQuery = q.Encode()
   122  	}
   123  
   124  	// time stamps require urlencoded content header
   125  	setEncodedHeader(req)
   126  
   127  	// print/log the request if warranted
   128  	printHttpRequest(req, true)
   129  
   130  	res, err := client.Do(Config, req)
   131  	if err != nil {
   132  		return nil, err
   133  	}
   134  
   135  	// print/log the response if warranted
   136  	printHttpResponse(res, true)
   137  
   138  	if client.IsError(res) && res.StatusCode != 404 {
   139  		return nil, client.NewAPIError(res)
   140  	} else if res.StatusCode == 404 {
   141  		cErr := configgtm.CommonError{}
   142  		cErr.SetItem("entityName", "Property")
   143  		cErr.SetItem("name", propertyName)
   144  		return nil, cErr
   145  	} else {
   146  		err = client.BodyJSON(res, stat)
   147  		if err != nil {
   148  			return nil, err
   149  		}
   150  
   151  		return stat, nil
   152  	}
   153  }
   154  
   155  // GetTrafficPerProperty retrieves report traffic for the specified property in the specified domain.
   156  func GetTrafficPerProperty(domainName string, propertyName string, optArgs map[string]string) (*PropertyTrafficResponse, error) {
   157  	stat := &PropertyTrafficResponse{}
   158  	hostURL := fmt.Sprintf("/gtm-api/v1/reports/traffic/domains/%s/properties/%s", domainName, propertyName)
   159  
   160  	req, err := client.NewRequest(
   161  		Config,
   162  		"GET",
   163  		hostURL,
   164  		nil,
   165  	)
   166  	if err != nil {
   167  		return nil, err
   168  	}
   169  
   170  	// Look for and process optional query params
   171  	q := req.URL.Query()
   172  	for k, v := range optArgs {
   173  		switch k {
   174  		case "start":
   175  			fallthrough
   176  		case "end":
   177  			q.Add(k, v)
   178  		}
   179  	}
   180  	if optArgs != nil {
   181  		req.URL.RawQuery = q.Encode()
   182  	}
   183  
   184  	// time stamps require urlencoded content header
   185  	setEncodedHeader(req)
   186  
   187  	// print/log the request if warranted
   188  	printHttpRequest(req, true)
   189  
   190  	res, err := client.Do(Config, req)
   191  	if err != nil {
   192  		return nil, err
   193  	}
   194  
   195  	// print/log the response if warranted
   196  	printHttpResponse(res, true)
   197  
   198  	if client.IsError(res) && res.StatusCode != 404 {
   199  		return nil, client.NewAPIError(res)
   200  	} else if res.StatusCode == 404 {
   201  		cErr := configgtm.CommonError{}
   202  		cErr.SetItem("entityName", "Property")
   203  		cErr.SetItem("name", propertyName)
   204  		return nil, cErr
   205  	} else {
   206  		err = client.BodyJSON(res, stat)
   207  		if err != nil {
   208  			return nil, err
   209  		}
   210  
   211  		return stat, nil
   212  	}
   213  }