简体   繁体   中英

Terraform Custom Provider - Data Source

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...

错误

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM