github.com/akamai/AkamaiOPEN-edgegrid-golang/v2@v2.17.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://developer.akamai.com/api/core_features/datastream2_config/v1.html#getproperties 19 GetProperties(context.Context, GetPropertiesRequest) ([]Property, error) 20 21 // GetPropertiesByGroup returns properties that are active on the production and staging network and available within a specific group 22 // 23 // See: https://developer.akamai.com/api/core_features/datastream2_config/v1.html#getpropertiesbygroup 24 GetPropertiesByGroup(context.Context, GetPropertiesByGroupRequest) ([]Property, error) 25 26 // GetDatasetFields returns groups of data set fields available in the template. 27 // 28 // See: https://developer.akamai.com/api/core_features/datastream2_config/v1.html#gettemplatename 29 GetDatasetFields(context.Context, GetDatasetFieldsRequest) ([]DataSets, error) 30 } 31 32 // GetPropertiesRequest contains parameters necessary to send a GetProperties request 33 GetPropertiesRequest struct { 34 GroupId int 35 ProductId string 36 } 37 38 // GetPropertiesByGroupRequest contains parameters necessary to send a GetPropertiesByGroup request 39 GetPropertiesByGroupRequest struct { 40 GroupId int 41 } 42 43 // GetDatasetFieldsRequest contains parameters necessary to send a GetDatasetFields request 44 GetDatasetFieldsRequest struct { 45 TemplateName TemplateName 46 } 47 ) 48 49 // Validate performs validation on GetPropertiesRequest 50 func (r GetPropertiesRequest) Validate() error { 51 return validation.Errors{ 52 "GroupId": validation.Validate(r.GroupId, validation.Required), 53 "ProductId": validation.Validate(r.ProductId, validation.Required), 54 }.Filter() 55 } 56 57 // Validate performs validation on GetPropertiesRequest 58 func (r GetPropertiesByGroupRequest) Validate() error { 59 return validation.Errors{ 60 "GroupId": validation.Validate(r.GroupId, validation.Required), 61 }.Filter() 62 } 63 64 // Validate performs validation on GetDatasetFieldsRequest 65 func (r GetDatasetFieldsRequest) Validate() error { 66 return validation.Errors{ 67 "TemplateName": validation.Validate(r.TemplateName, validation.Required, validation.In(TemplateNameEdgeLogs)), 68 }.Filter() 69 } 70 71 var ( 72 // ErrGetProperties is returned when GetProperties fails 73 ErrGetProperties = errors.New("list properties") 74 // ErrGetPropertiesByGroup is returned when GetPropertiesByGroup fails 75 ErrGetPropertiesByGroup = errors.New("list properties by group") 76 // ErrGetDatasetFields is returned when GetDatasetFields fails 77 ErrGetDatasetFields = errors.New("list data set fields") 78 ) 79 80 func (d *ds) GetProperties(ctx context.Context, params GetPropertiesRequest) ([]Property, error) { 81 logger := d.Log(ctx) 82 logger.Debug("GetProperties") 83 84 if err := params.Validate(); err != nil { 85 return nil, fmt.Errorf("%s: %w: %s", ErrGetProperties, ErrStructValidation, err) 86 } 87 88 uri, err := url.Parse(fmt.Sprintf( 89 "/datastream-config-api/v1/log/properties/product/%s/group/%d", 90 params.ProductId, params.GroupId)) 91 if err != nil { 92 return nil, fmt.Errorf("%w: parsing URL: %s", ErrGetProperties, err) 93 } 94 95 req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri.String(), nil) 96 if err != nil { 97 return nil, fmt.Errorf("%w: failed to create request: %s", ErrGetProperties, err) 98 } 99 100 var rval []Property 101 resp, err := d.Exec(req, &rval) 102 if err != nil { 103 return nil, fmt.Errorf("%w: request failed: %s", ErrGetProperties, err) 104 } 105 106 if resp.StatusCode != http.StatusOK { 107 return nil, fmt.Errorf("%s: %w", ErrGetProperties, d.Error(resp)) 108 } 109 110 return rval, nil 111 } 112 113 func (d *ds) GetPropertiesByGroup(ctx context.Context, params GetPropertiesByGroupRequest) ([]Property, error) { 114 logger := d.Log(ctx) 115 logger.Debug("GetPropertiesByGroup") 116 117 if err := params.Validate(); err != nil { 118 return nil, fmt.Errorf("%s: %w: %s", ErrGetPropertiesByGroup, ErrStructValidation, err) 119 } 120 121 uri, err := url.Parse(fmt.Sprintf( 122 "/datastream-config-api/v1/log/properties/group/%d", 123 params.GroupId)) 124 if err != nil { 125 return nil, fmt.Errorf("%w: parsing URL: %s", ErrGetPropertiesByGroup, err) 126 } 127 128 req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri.String(), nil) 129 if err != nil { 130 return nil, fmt.Errorf("%w: failed to create request: %s", ErrGetPropertiesByGroup, err) 131 } 132 133 var rval []Property 134 resp, err := d.Exec(req, &rval) 135 if err != nil { 136 return nil, fmt.Errorf("%w: request failed: %s", ErrGetPropertiesByGroup, err) 137 } 138 139 if resp.StatusCode != http.StatusOK { 140 return nil, fmt.Errorf("%s: %w", ErrGetPropertiesByGroup, d.Error(resp)) 141 } 142 143 return rval, nil 144 } 145 146 func (d *ds) GetDatasetFields(ctx context.Context, params GetDatasetFieldsRequest) ([]DataSets, error) { 147 logger := d.Log(ctx) 148 logger.Debug("GetDatasetFields") 149 150 if err := params.Validate(); err != nil { 151 return nil, fmt.Errorf("%s: %w: %s", ErrGetDatasetFields, ErrStructValidation, err) 152 } 153 154 uri, err := url.Parse(fmt.Sprintf( 155 "/datastream-config-api/v1/log/datasets/template/%s", 156 params.TemplateName)) 157 if err != nil { 158 return nil, fmt.Errorf("%w: parsing URL: %s", ErrGetDatasetFields, err) 159 } 160 161 req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri.String(), nil) 162 if err != nil { 163 return nil, fmt.Errorf("%w: failed to create request: %s", ErrGetDatasetFields, err) 164 } 165 166 var rval []DataSets 167 resp, err := d.Exec(req, &rval) 168 if err != nil { 169 return nil, fmt.Errorf("%w: request failed: %s", ErrGetDatasetFields, err) 170 } 171 172 if resp.StatusCode != http.StatusOK { 173 return nil, fmt.Errorf("%s: %w", ErrGetDatasetFields, d.Error(resp)) 174 } 175 176 return rval, nil 177 }