Skip to content

Commit fe9a8c1

Browse files
authored
feat(apiovh): handle credentials template (#489)
Signed-off-by: William Poussier <[email protected]>
1 parent cfbc8ee commit fe9a8c1

File tree

1 file changed

+25
-17
lines changed

1 file changed

+25
-17
lines changed

pkg/plugins/builtin/apiovh/apiovh.go

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@ import (
55
"encoding/json"
66
"fmt"
77
"net/url"
8+
"strings"
9+
"text/template"
810

911
"github.com/ovh/configstore"
1012
"github.com/ovh/go-ovh/ovh"
13+
1114
"github.com/ovh/utask/pkg/plugins/builtin/httputil"
1215
"github.com/ovh/utask/pkg/plugins/taskplugin"
1316
"github.com/ovh/utask/pkg/utils"
@@ -34,7 +37,7 @@ type APIOVHConfig struct {
3437
Body string `json:"body,omitempty"`
3538
}
3639

37-
// ovhConfig holds the the credentials needed to instantiate
40+
// ovhConfig holds the credentials needed to instantiate
3841
// an OVH API client
3942
type ovhConfig struct {
4043
Endpoint string `json:"endpoint"`
@@ -51,25 +54,30 @@ func validConfig(config interface{}) error {
5154
default:
5255
return fmt.Errorf("unknown method for gw runner: %q", cfg.Method)
5356
}
57+
// If the API credentials is a template, try to parse it.
58+
if strings.Index(cfg.Credentials, "{{") == -1 {
59+
ovhCfgStr, err := configstore.GetItemValue(cfg.Credentials)
60+
if err != nil {
61+
return fmt.Errorf("can't retrieve credentials from configstore: %s", err)
62+
}
5463

55-
ovhCfgStr, err := configstore.GetItemValue(cfg.Credentials)
56-
if err != nil {
57-
return fmt.Errorf("can't retrieve credentials from configstore: %s", err)
58-
}
59-
60-
var ovhcfg ovhConfig
61-
if err := json.Unmarshal([]byte(ovhCfgStr), &ovhcfg); err != nil {
62-
return fmt.Errorf("can't unmarshal ovhConfig from configstore: %s", err)
63-
}
64+
var ovhcfg ovhConfig
65+
if err := json.Unmarshal([]byte(ovhCfgStr), &ovhcfg); err != nil {
66+
return fmt.Errorf("can't unmarshal ovhConfig from configstore: %s", err)
67+
}
6468

65-
if _, err := ovh.NewClient(
66-
ovhcfg.Endpoint,
67-
ovhcfg.AppKey,
68-
ovhcfg.AppSecret,
69-
ovhcfg.ConsumerKey); err != nil {
70-
return fmt.Errorf("can't create new OVH client: %s", err)
69+
if _, err := ovh.NewClient(
70+
ovhcfg.Endpoint,
71+
ovhcfg.AppKey,
72+
ovhcfg.AppSecret,
73+
ovhcfg.ConsumerKey); err != nil {
74+
return fmt.Errorf("can't create new OVH client: %s", err)
75+
}
76+
} else {
77+
if _, err := template.New("credentials").Parse(cfg.Credentials); err != nil {
78+
return fmt.Errorf("failed to parse credentials template: %w", err)
79+
}
7180
}
72-
7381
return nil
7482
}
7583

0 commit comments

Comments
 (0)