99 "strings"
1010 "time"
1111
12+ "github.com/aohorodnyk/uid"
13+
1214 "github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1315 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1416
@@ -26,7 +28,7 @@ func resourceTask() *schema.Resource {
2628 "name" : {
2729 Type : schema .TypeString ,
2830 ForceNew : true ,
29- Required : true ,
31+ Optional : true ,
3032 },
3133 "cloud" : {
3234 Type : schema .TypeString ,
@@ -162,14 +164,15 @@ func resourceTaskCreate(ctx context.Context, d *schema.ResourceData, m interface
162164 return diagnostic (diags , err , diag .Error )
163165 }
164166
165- if err := task .Create (ctx ); err == nil {
166- d . SetId ( task . GetIdentifier ( ctx ). Long ())
167- } else {
167+ d . SetId ( task .GetIdentifier (ctx ). Long ())
168+
169+ if err := task . Create ( ctx ); err != nil {
168170 diags = diagnostic (diags , err , diag .Error )
169- if err := task .Delete (ctx ); err == nil {
170- diags = diagnostic (diags , errors .New ("failed to create" ), diag .Error )
171- } else {
171+ if err := task .Delete (ctx ); err != nil {
172172 diags = diagnostic (diags , err , diag .Error )
173+ } else {
174+ diags = diagnostic (diags , errors .New ("failed to create" ), diag .Error )
175+ d .SetId ("" )
173176 }
174177 }
175178
@@ -316,7 +319,22 @@ func resourceTaskBuild(ctx context.Context, d *schema.ResourceData, m interface{
316319 Parallelism : uint16 (d .Get ("parallelism" ).(int )),
317320 }
318321
319- return task .New (ctx , c , common .Identifier (d .Get ("name" ).(string )), t )
322+ name := d .Id ()
323+ if name == "" {
324+ if identifier := d .Get ("name" ).(string ); identifier != "" {
325+ name = identifier
326+ } else if identifier := os .Getenv ("GITHUB_RUN_ID" ); identifier != "" {
327+ name = identifier
328+ } else if identifier := os .Getenv ("CI_PIPELINE_ID" ); identifier != "" {
329+ name = identifier
330+ } else if identifier := os .Getenv ("BITBUCKET_STEP_TRIGGERER_UUID" ); identifier != "" {
331+ name = identifier
332+ } else {
333+ name = uid .NewProvider36Size (8 ).MustGenerate ().String ()
334+ }
335+ }
336+
337+ return task .New (ctx , c , common .Identifier (name ), t )
320338}
321339
322340func diagnostic (diags diag.Diagnostics , err error , severity diag.Severity ) diag.Diagnostics {
0 commit comments