Skip to content
This repository has been archived by the owner on Nov 20, 2020. It is now read-only.

Fetch machine details #45

Open
wants to merge 21 commits into
base: master
Choose a base branch
from
Open

Conversation

virajindasrao
Copy link
Contributor

This code will allow a user to update terraform configuration at deployment level after deployment succeed. To get field values of deployment, a user has to keep resource_configuration value equals to an empty string.
After request success, details will be fetched on terraform refresh.

Signed-off-by: Viraj Indasrao [email protected]

@virajindasrao virajindasrao requested a review from markpeek April 17, 2018 10:14
Copy link

@raghavav raghavav left a comment

Choose a reason for hiding this comment

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

It would be really useful to have an example blueprint in this review. It would also be good to add comments in the code about the structure of the template retrieved from the catalog item. This would make it much easier to read the review and to check for assumptions.

@g1ps
Copy link

g1ps commented May 18, 2018

I grabbed actions.go and resource.go from Viraj's pull request and re-ran dep, re-compiled, re-installed, re-ran init and apply but show doesn't return anything different. The example files aren't necessary for compilation, are they? What have I missed?

Thanks.

Update: I added the examples for completeness. No change.

Update 2: I pulled the entire branch and built that. No change. It doesn't work at all, even when providing the empty ip_address var I later found in an example file.

@g1ps
Copy link

g1ps commented May 23, 2018

Any help here?? How do I get this working?

Thanks.

@raghavav
Copy link

raghavav commented May 23, 2018

Viraj is on a personal time off for this week. He should be able to get to this shortly. I have not had a chance to play with this pull request yet. I will start trying this change (as well as reviewing it) shortly.
No the examples are not necessary for functionality.

@g1ps
Copy link

g1ps commented May 24, 2018

Thank you, @raghavav.

@DanielVaknin
Copy link

Is there an update on this PR?

Thanks.

@virajindasrao
Copy link
Contributor Author

Working on it.


"encoding/json"

"github.com/hashicorp/terraform/helper/schema"
)

//ResourceViewsTemplate - is used to store information
//related to resource template information.
type ResourceViewsTemplate struct {
Copy link

Choose a reason for hiding this comment

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

Rename this type. It is not a 'template'.
It is just 'Resource' or 'ResourceView'.

i know the same change is happening in PR #27. I think it is worth pulling that rename change here.

usedConfigKeys = append(usedConfigKeys, configKey)
} else {
log.Printf("%s was not replaced", configKey)
if resourceConfiguration[configKey] != nil && resourceConfiguration[configKey] != "" {
Copy link

Choose a reason for hiding this comment

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

Not worth the additional indent here.
Just use

if resourceConfiguration[configKey] == nil || resourceConfiguration[configKey] == "" {
break;
}

Better yet, create an 'isEmpty(string) boolean utility function. There you can also check for other types of empty strings. That function will likely need to be reused in other places too.

@@ -394,6 +396,135 @@ func updateResource(d *schema.ResourceData, meta interface{}) error {
return nil
}

func fetchResourceFieldsValues(d *schema.ResourceData, meta interface{}) error {
//Get requester machine ID from schema.dataresource
requestMachineID := d.Id()
Copy link

Choose a reason for hiding this comment

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

Rename field to 'catalogRequestId'.

//Get client handle
client := meta.(*APIClient)
//Fetch resource details from vRA
templateResources, errTemplate := client.GetResourceViews(requestMachineID)
Copy link

Choose a reason for hiding this comment

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

not 'remplateResources'.
Call it 'resourceView'

"{com.vmware.csp.component.iaas.proxy.provider@resource.action.name.machine.Reconfigure}"

//Read vRA resource content one by one
for item := range templateResources.Content {
Copy link

Choose a reason for hiding this comment

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

for index, component := range templateResources.Content

@@ -394,6 +396,135 @@ func updateResource(d *schema.ResourceData, meta interface{}) error {
return nil
}

func fetchResourceFieldsValues(d *schema.ResourceData, meta interface{}) error {
//Get requester machine ID from schema.dataresource
Copy link

Choose a reason for hiding this comment

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

PR #27 has an implementation of this same method. Can you please rationalize the two PRs? I am not sure which one you want to go forward with.

I will put in some review comments here, but I think the two look different and you have to decide which way you want to go. In my opinion it is best to go with the version in PR #27.

//Iterate over TF resource > resource_configuration
for configKey := range resourceConfiguration {
//Check if any TF resource > resource_configuration is empty or null
if resourceConfiguration[configKey] == nil || resourceConfiguration[configKey] == "" {
Copy link

Choose a reason for hiding this comment

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

There is clearly an opportunity here to use that 'isEmpty(string) boolean' utility function that I mentioned earlier in this review.

//Check if any TF resource > resource_configuration is empty or null
if resourceConfiguration[configKey] == nil || resourceConfiguration[configKey] == "" {
//Compare vRA child resource and TF resource > resource_configuration names
if strings.Contains(configKey, componentName+".") {
Copy link

Choose a reason for hiding this comment

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

Should be using HasPrefix and TrimPrefix functions as mentioned in the reviews in PR #55

if returnValue != nil {
//If value returned from getTemplateValue is in float64 format then
//convert it in string format
if reflect.ValueOf(returnValue).Kind() == reflect.Float64 {
Copy link

Choose a reason for hiding this comment

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

As mentioned in the review in PR #27, please fold all this type conversions directly into the getTemplateValue function. That function can return a string instead of returning an interface{}.
Also, I like the idea of that function telling you if a value has changed.

//If value type is map then set recursive call which will fiend field in one level down of map interface
if reflect.ValueOf(templateInterface[i]).Kind() == reflect.Map {
template, _ := templateInterface[i].(map[string]interface{})
returnValue := getTemplateValue(template, field)
Copy link

Choose a reason for hiding this comment

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

As I said in the review for PR #27, I am not sure what the use-case is for this kind of a recursive retrieval from multiple levels of a map. I think it is not necessary or even a good idea.

got vet
Signed-off-by: Viraj Indasrao <[email protected]>
Signed-off-by: Viraj Indasrao <[email protected]>
Minor fixes
Signed-off-by: Viraj Indasrao <[email protected]>
Signed-off-by: Viraj Indasrao <[email protected]>
Signed-off-by: Viraj Indasrao <[email protected]>
Signed-off-by: Viraj Indasrao <[email protected]>
@vmwclabot
Copy link

@virajindasrao, you must sign every commit in this pull request acknowledging our Developer Certificate of Origin before your changes are merged. This can be done by adding Signed-off-by: John Doe <[email protected]> to the last line of each Git commit message. The e-mail address used to sign must match the e-mail address of the Git author. Click here to view the Developer Certificate of Origin agreement.

got vet
Signed-off-by: Viraj Indasrao <[email protected]>
Signed-off-by: Viraj Indasrao <[email protected]>
Minor fixes
Signed-off-by: Viraj Indasrao <[email protected]>
Signed-off-by: Viraj Indasrao <[email protected]>
Signed-off-by: Viraj Indasrao <[email protected]>
@virajindasrao virajindasrao force-pushed the viraj-fetch-machine-details branch from 2e463f4 to 7bd4d8f Compare June 14, 2018 13:03
@vmwclabot
Copy link

@virajindasrao, you must sign every commit in this pull request acknowledging our Developer Certificate of Origin before your changes are merged. This can be done by adding Signed-off-by: John Doe <[email protected]> to the last line of each Git commit message. The e-mail address used to sign must match the e-mail address of the Git author. Click here to view the Developer Certificate of Origin agreement.

Signed-off-by: Viraj Indasrao <[email protected]>
@virajindasrao virajindasrao force-pushed the viraj-fetch-machine-details branch from b1d0298 to ca55172 Compare June 27, 2018 12:26
Signed-off-by: Viraj Indasrao <[email protected]>
Signed-off-by: Viraj Indasrao <[email protected]>
Signed-off-by: Viraj Indasrao <[email protected]>
@g1ps
Copy link

g1ps commented Jul 5, 2018

This now works for me using the master branch, passing the empty ip_address var in resource_configuration and setting the variable.

output "ip" {
  value = "${vra7_resource.machine2.resource_configuration.RHEL7.4.ip_address}"
}
...
resource "vra7_resource" "machine2" {
  catalog_name = "Red Hat Enterprise Linux 7.4"

  resource_configuration = {
    RHEL7.4.cpu         = "2"
    RHEL7.4.memory      = "4096"
    RHEL7.4.description = "test"
    RHEL7.4.ip_address  = ""
  }
...

Thanks.

UPDATE: Spoke too soon. It worked once. See #17. 😦

@vmware-archive vmware-archive deleted a comment from vmwclabot Jul 5, 2018
Signed-off-by: Viraj Indasrao <[email protected]>
@@ -274,12 +275,17 @@ func createResource(d *schema.ResourceData, meta interface{}) error {
//Update request template field values with values from user configuration.
resourceConfiguration, _ := d.Get("resource_configuration").(map[string]interface{})
for configKey, configValue := range resourceConfiguration {
if len(configValue.(string)) == 0 {
break
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 continue instead of break.

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.

6 participants