Terraform Custom Provider - Data Source

橙三吉。 提交于 2020-04-06 04:12:12

问题


I am currently working on a custom Terraform Provider for Jumpcloud and am experiencing some challenges with configuring a custom data source. I see that the Resource schema requires Create, Read, Update, and delete.

I am just wanting to run an API query in Terraform so that I can return a list of users and use those users in a for loop.

Below is the API call that I have tagged as a schema.Resource

func userquery() *schema.Resource {
    apiKey := ""
    userGroupID := ""

    contentType := "application/json"
    accept := "application/json"

    // Instantiate the API client
    client := jcapiv2.NewAPIClient(jcapiv2.NewConfiguration())

    // Set up the API key via context
    auth := context.WithValue(context.TODO(), jcapiv2.ContextAPIKey, jcapiv2.APIKey{
        Key: apiKey,
    })

    // Make an API call to retrieve a specific user group by ID
    userGroup, res, err := client.UserGroupsApi.GroupsUserGet(auth, userGroupID, contentType, accept, nil)
    if err != nil {
        fmt.Printf("Error retrieving user group %s: %s - response = %+v\n", userGroupID, err, res)
    } else {
        fmt.Printf("Details for User group %s: %+v\n", userGroupID, userGroup)
    }

    return nil

}

This is the mapping

ResourcesMap: map[string]*schema.Resource{
            "jumpcloud_user":                  resourceUser(),
            "jumpcloud_user_group":            resourceUserGroup(),
            "jumpcloud_user_group_membership": resourceUserGroupMembership(),
            "jumpcloud_user_query":            userquery(),

        },

And this is the error that I am receiving. What is interesting is that this is returning the name correctly from the API call however, there is still an error...


回答1:


I don't have much experience working with Terraform but forwarded this thread to a coworker who does and this is what they sent me

"The user is trying to build a custom datasource for his custom JC provider instead of a resource. I've had minimal experience with this, however, his error seems like there's an issue with how he's building his plugin but there is no information on what version of terraform or golang he is using so I'm would be unable to help there. I would suggest referencing the custom JC provider (https://github.com/sagewave/terraform-provider-jumpcloud) since they have some custom data sources built but there aren't any custom data sources that are jumpcloud specific. Hope that helps."

Have you checked out this JC terraform provider to see if it will fit your use case?

https://github.com/sagewave/terraform-provider-jumpcloud

Cheers



来源:https://stackoverflow.com/questions/60897296/terraform-custom-provider-data-source

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!