ocket8888 commented on a change in pull request #3930: Rewrite profile import 
to Go
URL: https://github.com/apache/trafficcontrol/pull/3930#discussion_r331138332
 
 

 ##########
 File path: lib/go-tc/profiles.go
 ##########
 @@ -89,30 +101,127 @@ type ProfileTrimmed struct {
        Name string `json:"name"`
 }
 
-type ProfileExportedNullable struct {
-       // The Profile name
-       //
-       Name *string `json:"name"`
-
-       // The Profile Description
-       //
+// ProfileExportImportNullable is an object of the form used by Traffic Ops
+// to represent exported and imported profiles.
+type ProfileExportImportNullable struct {
+       Name        *string `json:"name"`
        Description *string `json:"description"`
+       CDNName     *string `json:"cdn"`
+       Type        *string `json:"type"`
+}
 
-       // The CDN name associated with the Profile
+// ProfileExportResponse is an object of the form used by Traffic Ops
+// to represent exported profile response
+type ProfileExportResponse struct {
+       // Parameters associated to the profile
        //
-       CDNName *string `json:"cdn"`
+       Profile ProfileExportImportNullable `json:"profile"`
 
-       // The Type name associated with the Profile
+       // Parameters associated to the profile
        //
-       Type *string `json:"type"`
+       Parameters []ProfileExportImportParameterNullable `json:"parameters"`
 }
 
-type ProfileExportedResponse struct {
+// ProfileImportRequest is an object of the form used by Traffic Ops
+// to represent a request to import a profile
+type ProfileImportRequest struct {
        // Parameters associated to the profile
        //
-       Profile ProfileExportedNullable `json:"profile"`
+       Profile ProfileExportImportNullable `json:"profile"`
 
        // Parameters associated to the profile
        //
-       Parameters []ProfileExportedParameterNullable `json:"parameters"`
+       Parameters []ProfileExportImportParameterNullable `json:"parameters"`
+}
+
+// ProfileImportResponse is an object of the form used by Traffic Ops
+// to represent a response from importing a profile
+type ProfileImportResponse struct {
+       Response ProfileImportResponseObj `json:"response"`
+       Alerts
+}
+
+type ProfileImportResponseObj struct {
+       ProfileExportImportNullable
+       ID *int64 `json:"id"`
+}
+
+// Validate validates an profile import request
+func (profileImport *ProfileImportRequest) Validate(tx *sql.Tx) error {
+
+       profile := profileImport.Profile
+
+       // Profile fields are valid
+       errs := tovalidate.ToErrors(validation.Errors{
+               "name":        validation.Validate(profile.Name, 
validation.Required),
+               "description": validation.Validate(profile.Description, 
validation.Required),
+               "cdnName":     validation.Validate(profile.CDNName, 
validation.Required),
+               "type":        validation.Validate(profile.Type, 
validation.Required),
+       })
+
+       // Validate CDN exist
+       if profile.CDNName != nil {
+               if ok, err := CDNExistsByName(*profile.CDNName, tx); err != nil 
{
+                       errs = append(errs, fmt.Errorf("checking cdn name %v 
existence: %v", *profile.CDNName, err.Error()))
+               } else if !ok {
+                       errs = append(errs, fmt.Errorf("%v CDN does not exist", 
*profile.CDNName))
+               }
+       }
+
+       // Validate profile does not already exist
+       if profile.Name != nil {
+               if ok, err := ProfileExistsByName(*profile.Name, tx); err != 
nil {
+                       errs = append(errs, fmt.Errorf("checking profile name 
%v existence: %v", *profile.Name, err.Error()))
 
 Review comment:
   Database errors like this ought not be exposed to the user - I think you 
should change this to to a `log.Errorf` and return something generic as an 
error.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to