@@ -5,6 +5,8 @@ package provider
5
5
6
6
import (
7
7
"context"
8
+ "encoding/base64"
9
+ "encoding/json"
8
10
"fmt"
9
11
"github.com/hashicorp/terraform-plugin-framework/diag"
10
12
"github.com/hashicorp/terraform-plugin-framework/path"
@@ -26,6 +28,17 @@ var _ resource.Resource = &ServiceAccountResource{}
26
28
27
29
var defaultRoles = []string {"//iam.api.mondoo.app/roles/viewer" }
28
30
31
+ // serviceAccountCredential is a temporary object until the API returns the credential as a string.
32
+ type serviceAccountCredential struct {
33
+ Mrn string `json:"mrn,omitempty"`
34
+ PrivateKey string `json:"private_key,omitempty"`
35
+ Certificate string `json:"certificate,omitempty"`
36
+ ApiEndpoint string `json:"api_endpoint,omitempty"`
37
+ ScopeMrn string `json:"scope_mrn,omitempty"`
38
+ // ParentMrn is deprecated and should not be used, use ScopeMrn instead
39
+ ParentMrn string `json:"parent_mrn,omitempty"`
40
+ }
41
+
29
42
func NewServiceAccountResource () resource.Resource {
30
43
return & ServiceAccountResource {}
31
44
}
@@ -46,6 +59,9 @@ type ServiceAccountResourceModel struct {
46
59
Name types.String `tfsdk:"name"`
47
60
Description types.String `tfsdk:"description"`
48
61
Roles types.List `tfsdk:"roles"`
62
+
63
+ // base 64 encoded service account credential
64
+ Credential types.String `tfsdk:"credential"`
49
65
}
50
66
51
67
func (r * ServiceAccountResource ) Metadata (ctx context.Context , req resource.MetadataRequest , resp * resource.MetadataResponse ) {
@@ -99,6 +115,14 @@ func (r *ServiceAccountResource) Schema(ctx context.Context, req resource.Schema
99
115
listplanmodifier .UseStateForUnknown (),
100
116
},
101
117
},
118
+ "credential" : schema.StringAttribute {
119
+ Computed : true ,
120
+ MarkdownDescription : "The service account credential in JSON format, base64 encoded. This is the same content when creating service account credentials through the web console." ,
121
+ PlanModifiers : []planmodifier.String {
122
+ stringplanmodifier .UseStateForUnknown (),
123
+ },
124
+ Sensitive : true ,
125
+ },
102
126
},
103
127
}
104
128
}
@@ -206,7 +230,25 @@ func (r *ServiceAccountResource) Create(ctx context.Context, req resource.Create
206
230
// Save space mrn into the Terraform state.
207
231
data .Name = types .StringValue (name )
208
232
data .Mrn = types .StringValue (string (createMutation .CreateServiceAccount .Mrn ))
209
- // TODO: add certificate and private key
233
+
234
+ // NOTE: this is temporary, we want to change the API to return the credential as a string
235
+ serviceAccount := serviceAccountCredential {
236
+ Mrn : string (createMutation .CreateServiceAccount .Mrn ),
237
+ PrivateKey : string (createMutation .CreateServiceAccount .PrivateKey ),
238
+ Certificate : string (createMutation .CreateServiceAccount .Certificate ),
239
+ ApiEndpoint : string (createMutation .CreateServiceAccount .ApiEndpoint ),
240
+ ScopeMrn : string (createMutation .CreateServiceAccount .ScopeMrn ),
241
+ ParentMrn : string (createMutation .CreateServiceAccount .ScopeMrn ),
242
+ }
243
+
244
+ jsonData , err := json .Marshal (serviceAccount )
245
+ if err != nil {
246
+ resp .Diagnostics .AddError ("Client Error" , fmt .Sprintf ("Unable to create service account, got error: %s" , err ))
247
+ return
248
+ }
249
+
250
+ // set base 64 encoded credential
251
+ data .Credential = types .StringValue (base64 .StdEncoding .EncodeToString (jsonData ))
210
252
211
253
// Write logs using the tflog package
212
254
tflog .Trace (ctx , "created a service account resource" )
0 commit comments