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

Fix import for compute_route #565

Merged
merged 5 commits into from
Oct 13, 2017

Conversation

rosbo
Copy link
Contributor

@rosbo rosbo commented Oct 10, 2017

Fixes one resource for #431

  • Reorder fields in schema for style consistency
  • Add reusable ZonalFieldValue
  • Import tests were not testing the right thing. Import was broken. I fixed the import.
  • Read state from the API
  • Generate network link without calling the API

@rosbo rosbo changed the title Compute route network field Fix import for compute_route. Oct 10, 2017
@rosbo rosbo changed the title Fix import for compute_route. Fix import for compute_route Oct 10, 2017
@rosbo rosbo requested a review from selmanj October 11, 2017 21:41
//
// If the project is not specified, it first tries to get the project from the `projectSchemaField` and then fallback on the default project.
// If the zone is not specified, it takes the value of `zoneSchemaField`.

Copy link
Contributor

Choose a reason for hiding this comment

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

empty space

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed

// If the project is not specified, it first tries to get the project from the `projectSchemaField` and then fallback on the default project.
// If the zone is not specified, it takes the value of `zoneSchemaField`.

func parseZonalFieldValue(resourceType, fieldValue, projectSchemaField, zoneSchemaField string, d TerraformResourceData, config *Config, isEmptyValid bool) (*ZonalFieldValue, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This function has 7 arguments. Is it doing too much? You might consider breaking it down into smaller functions or somehow grouping related attributes together. Up to you if you think that's simpler

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Then entry point would still require all the parameters. The logic using those parameters is extracted away in method like getProjectFromSchema.

Copy link
Contributor

@selmanj selmanj Oct 12, 2017

Choose a reason for hiding this comment

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

Well, not exactly. Some of these parameters aren't "request-level" (e.g. they don't change inside the specific resource's code) and could be extracted into a more general constructor. You could imagine something like

// Defined at top of resource
var zoneParser := ZoneParser{ResourceType: "networks", IsEmptyValid: false}

func myResourceCreate(d *schema.ResourceData, meta interface{}) {
    // a bunch of stuff happens here
    // [...]

    zoneParser.Parse(myFieldValue, "foo_field", "project", d, config)
}

Similar to how a regex's pattern is defined globally ahead of where it's actually used. But it's up to you; it may be this representation is uglier.

globalLinkTemplate = "projects/%s/global/%s/%s"
globalLinkBasePattern = "projects/(.+)/global/%s/(.+)"
globalLinkTemplate = "projects/%s/global/%s/%s"
globalLinkBasePattern = "projects/(.+)/global/%s/(.+)"
Copy link
Contributor

Choose a reason for hiding this comment

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

I still don't enjoy seeing a mixture of regexps and formatting patterns (when reading this it's not obvious to me whether the regexp is matched before or after the print). However if this ends up reducing the amount of code needed (which I'm guessing it does given the large number of constants) then I'll defer to your judgement

Alternatively, consider inlining these (especially if they're only used once). Then the formatting/regexp matching is much more explicit

Copy link
Contributor Author

Choose a reason for hiding this comment

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

*Template is used in more than one place.
*BasePattern is used in only one place but I like to have both next to each other. It is easy to catch a mistake.

}

r = regexp.MustCompile(fmt.Sprintf(zonalPartialLinkBasePattern, resourceType))
if r.MatchString(fieldValue) {
Copy link
Contributor

Choose a reason for hiding this comment

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

You might be able to collapse this and the following line via:

if parts := r.FindStringSubmatch(fieldValue); parts != nil {
   // stuff happens
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@rosbo rosbo force-pushed the compute_route_network_field branch from 5554bd6 to d99baf8 Compare October 13, 2017 18:02
@rosbo rosbo merged commit 3f3fa86 into hashicorp:master Oct 13, 2017
@rosbo rosbo deleted the compute_route_network_field branch October 13, 2017 18:05
negz pushed a commit to negz/terraform-provider-google that referenced this pull request Oct 17, 2017
* Reorder fields in schema for style consistency
* Add reusable ZonalFieldValue
* Fix import and read state from API for compute route
* Generate network link without calling the API
chrisst pushed a commit to chrisst/terraform-provider-google that referenced this pull request Nov 9, 2018
* Reorder fields in schema for style consistency
* Add reusable ZonalFieldValue
* Fix import and read state from API for compute route
* Generate network link without calling the API
luis-silva pushed a commit to luis-silva/terraform-provider-google that referenced this pull request May 21, 2019
@ghost
Copy link

ghost commented Mar 30, 2020

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. If you feel I made an error 🤖 🙉 , please reach out to my human friends 👉 [email protected]. Thanks!

@ghost ghost locked and limited conversation to collaborators Mar 30, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants