2021-03-18 03:30:54 -04:00
|
|
|
package provider
|
2021-01-08 10:38:30 -05:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
|
|
|
|
|
2021-03-18 03:30:54 -04:00
|
|
|
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
|
2021-01-08 10:38:30 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func dataSourceDockerPlugin() *schema.Resource {
|
|
|
|
|
return &schema.Resource{
|
|
|
|
|
Read: dataSourceDockerPluginRead,
|
|
|
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
|
"id": {
|
|
|
|
|
Type: schema.TypeString,
|
|
|
|
|
Optional: true,
|
|
|
|
|
},
|
2021-03-18 03:30:54 -04:00
|
|
|
"name": {
|
|
|
|
|
Type: schema.TypeString,
|
|
|
|
|
Optional: true,
|
|
|
|
|
},
|
2021-01-08 10:38:30 -05:00
|
|
|
"alias": {
|
|
|
|
|
Type: schema.TypeString,
|
|
|
|
|
Optional: true,
|
|
|
|
|
Description: "Docker Plugin alias",
|
|
|
|
|
},
|
|
|
|
|
"plugin_reference": {
|
|
|
|
|
Type: schema.TypeString,
|
|
|
|
|
Description: "Docker Plugin Reference",
|
|
|
|
|
Computed: true,
|
|
|
|
|
},
|
|
|
|
|
"enabled": {
|
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
|
Computed: true,
|
|
|
|
|
},
|
|
|
|
|
"grant_all_permissions": {
|
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
|
Computed: true,
|
|
|
|
|
Description: "If true, grant all permissions necessary to run the plugin",
|
|
|
|
|
},
|
|
|
|
|
"env": {
|
|
|
|
|
Type: schema.TypeSet,
|
|
|
|
|
Computed: true,
|
|
|
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var errDataSourceKeyIsMissing = errors.New("One of id or alias must be assigned")
|
|
|
|
|
|
|
|
|
|
func getDataSourcePluginKey(d *schema.ResourceData) (string, error) {
|
|
|
|
|
id, idOK := d.GetOk("id")
|
|
|
|
|
alias, aliasOK := d.GetOk("alias")
|
|
|
|
|
if idOK {
|
|
|
|
|
if aliasOK {
|
|
|
|
|
return "", errDataSourceKeyIsMissing
|
|
|
|
|
}
|
|
|
|
|
return id.(string), nil
|
|
|
|
|
}
|
|
|
|
|
if aliasOK {
|
|
|
|
|
return alias.(string), nil
|
|
|
|
|
}
|
|
|
|
|
return "", errDataSourceKeyIsMissing
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func dataSourceDockerPluginRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
|
key, err := getDataSourcePluginKey(d)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
client := meta.(*ProviderConfig).DockerClient
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
plugin, _, err := client.PluginInspectWithRaw(ctx, key)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("inspect a Docker plugin "+key+": %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setDockerPlugin(d, plugin)
|
|
|
|
|
return nil
|
|
|
|
|
}
|