forked from integrations/terraform-provider-github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresource_github_actions_environment_secret_migration.go
More file actions
103 lines (89 loc) · 2.95 KB
/
resource_github_actions_environment_secret_migration.go
File metadata and controls
103 lines (89 loc) · 2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package github
import (
"context"
"fmt"
"log"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)
func resourceGithubActionsEnvironmentSecretV0() *schema.Resource {
return &schema.Resource{
SchemaVersion: 0,
Schema: map[string]*schema.Schema{
"repository": {
Type: schema.TypeString,
Required: true,
Description: "Name of the repository.",
},
"environment": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Name of the environment.",
},
"secret_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Name of the secret.",
ValidateDiagFunc: validateSecretNameFunc,
},
"encrypted_value": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: "Encrypted value of the secret using the GitHub public key in Base64 format.",
ConflictsWith: []string{"plaintext_value"},
ValidateDiagFunc: validation.ToDiagFunc(validation.StringIsBase64),
},
"plaintext_value": {
Type: schema.TypeString,
Optional: true,
Sensitive: true,
Description: "Plaintext value of the secret to be encrypted.",
ConflictsWith: []string{"encrypted_value"},
},
"created_at": {
Type: schema.TypeString,
Computed: true,
Description: "Date of 'actions_environment_secret' creation.",
},
"updated_at": {
Type: schema.TypeString,
Computed: true,
Description: "Date of 'actions_environment_secret' update.",
},
},
}
}
func resourceGithubActionsEnvironmentSecretStateUpgradeV0(ctx context.Context, rawState map[string]any, m any) (map[string]any, error) {
meta := m.(*Owner)
client := meta.v3client
owner := meta.name
log.Printf("[DEBUG] GitHub Actions Environment Secret Attributes before migration: %#v", rawState)
repoName, ok := rawState["repository"].(string)
if !ok {
return nil, fmt.Errorf("repository not found or is not a string")
}
envName, ok := rawState["environment"].(string)
if !ok {
return nil, fmt.Errorf("environment not found or is not a string")
}
secretName, ok := rawState["secret_name"].(string)
if !ok {
return nil, fmt.Errorf("secret_name not found or is not a string")
}
repo, _, err := client.Repositories.Get(ctx, owner, repoName)
if err != nil {
return nil, fmt.Errorf("failed to retrieve repository %s: %w", repoName, err)
}
repoID := int(repo.GetID())
id, err := buildID(repoName, escapeIDPart(envName), secretName)
if err != nil {
return nil, fmt.Errorf("failed to build id for repository %s, environment %s, secret %s: %w", repoName, envName, secretName, err)
}
rawState["id"] = id
rawState["repository_id"] = repoID
log.Printf("[DEBUG] GitHub Actions Environment Secret Attributes after migration: %#v", rawState)
return rawState, nil
}