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

     1  package datastream
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"fmt"
     7  	"net/http"
     8  	"net/url"
     9  
    10  	validation "github.com/go-ozzo/ozzo-validation/v4"
    11  )
    12  
    13  type (
    14  	// Properties is an interface for listing various DS API properties
    15  	Properties interface {
    16  		// GetProperties returns properties that are active on the production and staging network for a specific product type that are available within a group
    17  		//
    18  		// See: https://techdocs.akamai.com/datastream2/v2/reference/get-group-properties
    19  		GetProperties(context.Context, GetPropertiesRequest) (*PropertiesDetails, error)
    20  
    21  		// GetDatasetFields returns groups of data set fields available in the template.
    22  		//
    23  		// See: https://techdocs.akamai.com/datastream2/v2/reference/get-dataset-fields
    24  		GetDatasetFields(context.Context, GetDatasetFieldsRequest) (*DataSets, error)
    25  	}
    26  
    27  	// GetPropertiesRequest contains parameters necessary to send a GetProperties request
    28  	GetPropertiesRequest struct {
    29  		GroupId int
    30  	}
    31  
    32  	// GetDatasetFieldsRequest contains parameters necessary to send a GetDatasetFields request
    33  	GetDatasetFieldsRequest struct {
    34  		ProductID *string
    35  	}
    36  
    37  	// PropertiesDetails identifies the properties belong to the given group.
    38  	PropertiesDetails struct {
    39  		Properties []PropertyDetails `json:"properties"`
    40  		GroupID    int               `json:"groupId"`
    41  	}
    42  
    43  	// PropertyDetails identifies detailed info about the properties monitored in the stream.
    44  	PropertyDetails struct {
    45  		Hostnames    []string `json:"hostnames"`
    46  		ProductID    string   `json:"productId"`
    47  		ProductName  string   `json:"productName"`
    48  		PropertyID   int      `json:"propertyId"`
    49  		PropertyName string   `json:"propertyName"`
    50  		ContractID   string   `json:"contractId"`
    51  	}
    52  )
    53  
    54  // Validate performs validation on GetPropertiesRequest
    55  func (r GetPropertiesRequest) Validate() error {
    56  	return validation.Errors{
    57  		"GroupId": validation.Validate(r.GroupId, validation.Required),
    58  	}.Filter()
    59  }
    60  
    61  var (
    62  	// ErrGetProperties is returned when GetProperties fails
    63  	ErrGetProperties = errors.New("list properties")
    64  	// ErrGetDatasetFields is returned when GetDatasetFields fails
    65  	ErrGetDatasetFields = errors.New("list data set fields")
    66  )
    67  
    68  func (d *ds) GetProperties(ctx context.Context, params GetPropertiesRequest) (*PropertiesDetails, error) {
    69  	logger := d.Log(ctx)
    70  	logger.Debug("GetProperties")
    71  
    72  	if err := params.Validate(); err != nil {
    73  		return nil, fmt.Errorf("%s: %w: %s", ErrGetProperties, ErrStructValidation, err)
    74  	}
    75  
    76  	uri, err := url.Parse(fmt.Sprintf(
    77  		"/datastream-config-api/v2/log/groups/%d/properties",
    78  		params.GroupId))
    79  	if err != nil {
    80  		return nil, fmt.Errorf("%w: parsing URL: %s", ErrGetProperties, err)
    81  	}
    82  
    83  	req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri.String(), nil)
    84  	if err != nil {
    85  		return nil, fmt.Errorf("%w: failed to create request: %s", ErrGetProperties, err)
    86  	}
    87  
    88  	var rval PropertiesDetails
    89  	resp, err := d.Exec(req, &rval)
    90  	if err != nil {
    91  		return nil, fmt.Errorf("%w: request failed: %s", ErrGetProperties, err)
    92  	}
    93  
    94  	if resp.StatusCode != http.StatusOK {
    95  		return nil, fmt.Errorf("%s: %w", ErrGetProperties, d.Error(resp))
    96  	}
    97  
    98  	return &rval, nil
    99  }
   100  
   101  func (d *ds) GetDatasetFields(ctx context.Context, params GetDatasetFieldsRequest) (*DataSets, error) {
   102  	logger := d.Log(ctx)
   103  	logger.Debug("GetDatasetFields")
   104  
   105  	uri, err := url.Parse("/datastream-config-api/v2/log/datasets-fields")
   106  	if err != nil {
   107  		return nil, fmt.Errorf("%w: parsing URL: %s", ErrGetDatasetFields, err)
   108  	}
   109  
   110  	q := uri.Query()
   111  	if params.ProductID != nil {
   112  		q.Add("productId", fmt.Sprintf("%s", *params.ProductID))
   113  	}
   114  	uri.RawQuery = q.Encode()
   115  
   116  	req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri.String(), nil)
   117  	if err != nil {
   118  		return nil, fmt.Errorf("%w: failed to create request: %s", ErrGetDatasetFields, err)
   119  	}
   120  
   121  	var rval DataSets
   122  	resp, err := d.Exec(req, &rval)
   123  	if err != nil {
   124  		return nil, fmt.Errorf("%w: request failed: %s", ErrGetDatasetFields, err)
   125  	}
   126  
   127  	if resp.StatusCode != http.StatusOK {
   128  		return nil, fmt.Errorf("%s: %w", ErrGetDatasetFields, d.Error(resp))
   129  	}
   130  
   131  	return &rval, nil
   132  }