简体   繁体   English

Terraform 自定义提供程序 - 数据源

[英]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.我目前正在为 Jumpcloud 开发自定义 Terraform 提供程序,并且在配置自定义数据源时遇到了一些挑战。 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.我只想在 Terraform 中运行 API 查询,以便我可以返回用户列表并在 for 循环中使用这些用户。

Below is the API call that I have tagged as a schema.Resource下面是我标记为 schema.Resource 的 API 调用

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...有趣的是,这从 API 调用中正确返回了名称,但是仍然存在错误......

错误

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我没有太多使用 Terraform 的经验,但将此线程转发给了一位这样做的同事,这就是他们发给我的

"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." “用户正在尝试为其自定义 JC 提供程序而不是资源构建自定义数据源。我对此的经验很少,但是,他的错误似乎是他构建插件的方式存在问题,但没有关于他正在使用什么版本的 terraform 或 golang 所以我将无法在那里提供帮助。我建议参考自定义 JC 提供程序( https://github.com/sagewave/terraform-provider-jumpcloud ),因为他们有一些自定义构建了数据源,但没有任何特定于 jumpcloud 的自定义数据源。希望有帮助。”

Have you checked out this JC terraform provider to see if it will fit your use case?您是否查看过这个 JC terraform 提供程序以查看它是否适合您的用例?

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

Cheers干杯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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