Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed lastUpdated timestamp format in api v5 federation resolvers #7596

Merged
merged 26 commits into from
Jul 14, 2023

Conversation

gbkannan89
Copy link
Contributor

@gbkannan89 gbkannan89 commented Jun 23, 2023

Related: #5911

Fixes federation_resolver v5 apis to respond with RFC3339 date/time Format.


Which Traffic Control components are affected by this PR?

  • Documentation
  • Traffic Ops

What is the best way to verify this PR?

Make Api calls to types 5.0

Ferderation Resolver to Use RFC3339 Format

  --url https://localhost:8443/api/5.0/federation_resolvers


{ 
  "response": [
       {
		"id": 1,
		"ipAddress": "::1/1",
		"lastUpdated": "2019-11-06T15:18:14.952814+05:30",
		"type": "RESOLVE6"
	}
  ]
}

If this is a bugfix, which Traffic Control versions contained the bug?

-7.0.1

PR submission checklist

@ocket8888 ocket8888 added Traffic Ops related to Traffic Ops documentation related to documentation low impact affects only a small portion of a CDN, and cannot itself break one tech debt rework due to choosing easy/limited solution labels Jun 23, 2023
@gbkannan89 gbkannan89 marked this pull request as ready for review June 26, 2023 08:11
@codecov
Copy link

codecov bot commented Jun 26, 2023

Codecov Report

Merging #7596 (80001fe) into master (fabdce4) will increase coverage by 0.00%.
The diff coverage is 15.38%.

@@            Coverage Diff            @@
##             master    #7596   +/-   ##
=========================================
  Coverage     30.13%   30.13%           
  Complexity       98       98           
=========================================
  Files           794      794           
  Lines         84078    84054   -24     
  Branches        907      907           
=========================================
  Hits          25333    25333           
+ Misses        56615    56591   -24     
  Partials       2130     2130           
Flag Coverage Δ
golib_unit 48.26% <0.00%> (-0.04%) ⬇️
grove_unit 4.60% <ø> (ø)
t3c_unit 5.28% <ø> (ø)
traffic_monitor_unit 21.30% <ø> (ø)
traffic_ops_integration 69.42% <100.00%> (ø)
traffic_ops_unit 22.97% <0.00%> (+0.01%) ⬆️
traffic_portal_v2 73.78% <ø> (ø)
traffic_stats_unit 10.14% <ø> (ø)
unit_tests 27.26% <0.00%> (+<0.01%) ⬆️
v3 57.79% <ø> (ø)
v4 79.18% <ø> (ø)
v5 78.63% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
lib/go-tc/federation_resolver.go 45.45% <0.00%> (-31.47%) ⬇️
...olang/federation_resolvers/federation_resolvers.go 0.00% <0.00%> (ø)
traffic_ops/v5-client/federation_resolver.go 100.00% <100.00%> (ø)

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

CHANGELOG.md Outdated
@@ -59,6 +59,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
- [#7469](https://github.com/apache/trafficcontrol/pull/7469) *Traffic Ops* Changed logic to not report empty or missing cookies into TO error.log.
- [#7586](https://github.com/apache/trafficcontrol/pull/7586) *Traffic Ops* Add permission to Operations Role to read from dnsseckeys endpoint.
- [#7600](https://github.com/apache/trafficcontrol/pull/7600) *t3c* changed default go-direct command line arg to be old to avoid unexpected config changes upon upgrade.
- [#7596](https://github.com/apache/trafficcontrol/pull/7596) *Traffic Ops* Fixes `federation_resolvers` v5 apis to respond with `RFC3339` date/time Format.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a part of the "fixed" section.

@@ -50,6 +51,36 @@ type FederationResolver struct {
TypeID *uint `json:"typeId,omitempty" db:"type"`
}

// [V5] FederationResolverV5 is an alias for the Federal Resolver struct response used for the latest minor version associated with api major version 5.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The GoDoc comment should be gin with the name of the function/ struct that it applies to.

// [V5] FederationResolverV5 is an alias for the Federal Resolver struct response used for the latest minor version associated with api major version 5.
type FederationResolverV5 = FederationResolverV50

// [V50] FederationResolverV50 is used for RFC3339 format timestamp in FederationResolver which represents a resolver record for a CDN Federation.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment here about godocs.

}

// [V5] FederationResolversV5Response is an alias for the FederationResolvers struct response used for the latest minor version associated with api major version 5.
type FederationResolversV5Response = FederationResolversV50Response
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment here

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't look like this comment was addressed :)

// [V5] FederationResolversV5Response is an alias for the FederationResolvers struct response used for the latest minor version associated with api major version 5.
type FederationResolversV5Response = FederationResolversV50Response

// [V50] GET request to its /federation_resolvers endpoint for APIv5.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment here

type FederationResolversV5Response = FederationResolversV50Response

// [V50] GET request to its /federation_resolvers endpoint for APIv5.
type FederationResolversV50Response struct {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

V50 should be at the end of the name of the struct here

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one as well.

@@ -217,3 +217,14 @@ func StringIsValidFloat() *validation.StringRule {
return err == nil && !math.IsNaN(validated)
}, "must be a valid float")
}

// IsValidIPorCIDR returns whether the input is a valid IP address or CIDR notation subnet
func IsValidIPorCIDR(input string) bool {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: The name should be IsValidIPOrCIDR, instead of IsValidIPorCIDR
Note that the O needs to be capitalized

}

_, _, err := net.ParseCIDR(input)
return err == nil // Valid CIDR notation subnet
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we return the error from here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

now removed as i removed post method itself.

@@ -301,3 +306,153 @@ func deleteFederationResolver(inf *api.APIInfo) (tc.Alert, tc.FederationResolver

return alert, result, userErr, sysErr, statusCode
}

// GetFederationResolvers [V5] - get list of federation resolver for APIv5
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since federation_resolvers is not using the CRUDer interface, it will be much easier (and would involve lesser lines of code) if we could just check the api version in the Get and Create methods of federation_resolvers, and return the appropriate structs.
You can take a look at this PR for reference: #7547

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice, i would do this srijeet

@@ -466,8 +466,8 @@ func Routes(d ServerData) ([]Route, http.Handler, error) {
{Version: api.Version{Major: 5, Minor: 0}, Method: http.MethodDelete, Path: `federations/{id}/deliveryservices/{dsID}/?$`, Handler: api.DeleteHandler(&federations.TOFedDSes{}), RequiredPrivLevel: auth.PrivLevelAdmin, RequiredPermissions: []string{"FEDERATION:UPDATE", "DELIVERY-SERVICE:UPDATE", "FEDERATION:READ", "DELIVERY-SERVICE:READ"}, Authenticated: Authenticated, Middlewares: nil, ID: 441740257031},

// Federation Resolvers
{Version: api.Version{Major: 5, Minor: 0}, Method: http.MethodPost, Path: `federation_resolvers/?$`, Handler: federation_resolvers.Create, RequiredPrivLevel: auth.PrivLevelAdmin, RequiredPermissions: []string{"FEDERATION-RESOLVER:CREATE", "TYPE:READ"}, Authenticated: Authenticated, Middlewares: nil, ID: 413437366131},
{Version: api.Version{Major: 5, Minor: 0}, Method: http.MethodGet, Path: `federation_resolvers/?$`, Handler: federation_resolvers.Read, RequiredPrivLevel: auth.PrivLevelReadOnly, RequiredPermissions: []string{"FEDERATION-RESOLVER:READ", "TYPE:READ"}, Authenticated: Authenticated, Middlewares: nil, ID: 45660875931},
{Version: api.Version{Major: 5, Minor: 0}, Method: http.MethodPost, Path: `federation_resolvers/?$`, Handler: federation_resolvers.CreateFederationResolver, RequiredPrivLevel: auth.PrivLevelAdmin, RequiredPermissions: []string{"FEDERATION-RESOLVER:CREATE", "TYPE:READ"}, Authenticated: Authenticated, Middlewares: nil, ID: 413437366131},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you just used the same functions with the api version checks in there, you wouldn't need to change anything in this file.

@srijeet0406
Copy link
Contributor

Also, could you please change the title of the PR to be something like
Fixed lastUpdated timestamp format in api v5 federation resolvers

@gbkannan89 gbkannan89 changed the title Federation resolvers v5 Fixed lastUpdated timestamp format in api v5 federation resolvers Jul 6, 2023
@@ -50,6 +51,36 @@ type FederationResolver struct {
TypeID *uint `json:"typeId,omitempty" db:"type"`
}

// FederationResolverV5 [V5] - is an alias for the Federal Resolver struct response used for the latest minor version associated with APIv5.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Do we need the text within the brackets?

// FederationResolverV5 [V5] - is an alias for the Federal Resolver struct response used for the latest minor version associated with APIv5.
type FederationResolverV5 = FederationResolverV50

// FederationResolverV50 [V50]- is used for RFC3339 format timestamp in FederationResolver which represents a resolver record for a CDN Federation for APIv50.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment here

}

// [V5] FederationResolversV5Response is an alias for the FederationResolvers struct response used for the latest minor version associated with api major version 5.
type FederationResolversV5Response = FederationResolversV50Response
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't look like this comment was addressed :)

type FederationResolversV5Response = FederationResolversV50Response

// [V50] GET request to its /federation_resolvers endpoint for APIv5.
type FederationResolversV50Response struct {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one as well.

}

// [V5] FederationResolverV5Response represents struct response used for the latest minor version associated with api major version 5.
type FederationResolverV5Response = FederationResolverV50Response
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

@@ -103,6 +101,7 @@ func Create(w http.ResponseWriter, r *http.Request) {
}

// Read is the handler for GET requests to /federation_resolvers (and /federation_resolvers/{{ID}}).
// here based on request V5 is identified to get list of federation resolver for APIv5
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you need this godoc comment here.


// Based on version we load types - for version 5 and above we use FederationResolverV5
var resolvers []interface{}
if inf.Version != nil && inf.Version.Major >= 5 && inf.Version.Minor >= 0 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fine, but there is a utility function that can be used in place of this if statement like this:
if inf.Version.GreaterThanOrEqualTo(&api.Version{Major: 5, Minor: 0})

for _, item := range frData {
resolvers = append(resolvers, item)
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This entire code block is not needed, because, on lines 142 and 147, you're declaring a new variable (array), and on the subsequent lines, you're traversing over that array. There are 0 entries in the array, so traversing through it is needless.

if sysErr != nil {
sysErr = fmt.Errorf("federation_resolver scanning: %v", sysErr)
// Based on version we load types - for version 5 and above we use FederationResolverV5
if inf.Version != nil && inf.Version.Major >= 5 && inf.Version.Minor >= 0 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could use the pre defined utility function here, but it's your call.

}
api.HandleErr(w, r, tx, errCode, userErr, sysErr)
return
resolvers = append(resolvers, resolver)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of repeating this block twice, here's what I'd do:
Scan the struct into a v4 struct as it was being done earlier. Then, check for the version, and if it's >= 5.0, then convert the v4 struct into a v5 struct before appending it to the interface array resolvers.

var v5Resolver tc.FederationResolverV5

// Convert FederationResolver fields to FederationResolverV5 fields
v5Resolver.ID = resolver.ID
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good. Do you mind making it into a utility function please, so that future development can make use of it and not have to repeat these lines everywhere?

v5Resolver.LastUpdated = &lastUpdated
v5Resolver.Type = resolver.Type

resolvers = append(resolvers, &v5Resolver)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be appending the reference of v5Resolver?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here we are trying to append value of v5Resolver to resolvers, is that done wrongly ?

Copy link
Contributor

@srijeet0406 srijeet0406 Jul 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It needs to append the value of v5Resolver, not the reference (address), which is what the & does.
So, it should just be

resolvers = append(resolvers, v5Resolver)

Copy link
Contributor

@srijeet0406 srijeet0406 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! Manual testing also passes.

@srijeet0406 srijeet0406 merged commit db67a94 into apache:master Jul 14, 2023
@rimashah25 rimashah25 added this to the 8.0.0 milestone Jul 17, 2023
jagan-parthiban pushed a commit to jagan-parthiban/trafficcontrol that referenced this pull request Jul 27, 2023
…ache#7596)

* fr changes

* fr changes

* changelog added

* type import correction

* fr codefix

* fr code changes

* comment correction

* fixes

* comments addressed

* changelog correction

* fr changed to give rfc format res

* removed unused function

* version revert for deletion

* space added

* comments addressed

* comments addressed

* reference mistake correction
@gbkannan89 gbkannan89 deleted the federation-resolvers-v5 branch August 28, 2023 22:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation related to documentation low impact affects only a small portion of a CDN, and cannot itself break one tech debt rework due to choosing easy/limited solution Traffic Ops related to Traffic Ops
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants