@@ -125,27 +125,31 @@ func resourcePKICertCreate(_ context.Context, d *schema.ResourceData, m interfac
125125 }
126126 defer client .Close ()
127127
128+ roleName := d .Get ("role" ).(string )
128129 tenantID := d .Get ("tenant_id" ).(string )
129130 logicalPath , err := pki .APIEndpoint (tenantID ).LogicalPath ()
130131 if err != nil {
131132 return diag .FromErr (fmt .Errorf ("create PKI cert logicalPath: %w" , err ))
132133 }
134+
135+ // Only check role if we have a working consoleClient
133136 tenant , _ , err := client .Tenants .Retrieve (logicalPath )
134- if err != nil {
137+ if err == nil {
138+ _ , ok := tenant .GetRoleOk (roleName )
139+ if ! ok {
140+ return diag .FromErr (fmt .Errorf ("role '%s' not found or invalid" , roleName ))
141+ }
135142 return diag .FromErr (err )
136143 }
137- roleName := d . Get ( "role" ).( string )
144+
138145 ttl := d .Get ("ttl" ).(string )
139146 ipSANS := tools .ExpandStringList (d .Get ("ip_sans" ).(* schema.Set ).List ())
140147 uriSANS := tools .ExpandStringList (d .Get ("uri_sans" ).(* schema.Set ).List ())
141148 otherSANS := tools .ExpandStringList (d .Get ("other_sans" ).(* schema.Set ).List ())
142149 commonName := d .Get ("common_name" ).(string )
143150 altNames := d .Get ("alt_names" ).(string )
144151 excludeCNFromSANS := d .Get ("exclude_cn_from_sans" ).(bool )
145- role , ok := tenant .GetRoleOk (roleName )
146- if ! ok {
147- return diag .FromErr (fmt .Errorf ("role '%s' not found or invalid" , roleName ))
148- }
152+
149153 certRequest := pki.CertificateRequest {
150154 CommonName : commonName ,
151155 AltNames : altNames ,
@@ -157,7 +161,7 @@ func resourcePKICertCreate(_ context.Context, d *schema.ResourceData, m interfac
157161 PrivateKeyFormat : "pem" ,
158162 Format : "pem" ,
159163 }
160- cert , resp , err := client .Services .IssueCertificate (logicalPath , role . Name , certRequest )
164+ cert , resp , err := client .Services .IssueCertificate (logicalPath , roleName , certRequest )
161165 if err != nil {
162166 if resp != nil && resp .StatusCode == http .StatusForbidden {
163167 return diag .FromErr (fmt .Errorf ("you might be missing the 'PKI_CERT.ISSUE' permission for the tenant org: %w" , err ))
0 commit comments