From 978b79bf3fa2c8a8527ff9015ac5a94e798f21a5 Mon Sep 17 00:00:00 2001 From: Dmitry Starov Date: Fri, 6 Sep 2024 21:56:53 +0200 Subject: [PATCH 01/11] Update hashicorp/setup-terraform action --- .changes/unreleased/Security-20240906-215624.yaml | 3 +++ .github/workflows/test.yml | 4 ++-- 2 files changed, 5 insertions(+), 2 deletions(-) create mode 100644 .changes/unreleased/Security-20240906-215624.yaml diff --git a/.changes/unreleased/Security-20240906-215624.yaml b/.changes/unreleased/Security-20240906-215624.yaml new file mode 100644 index 0000000..54c190c --- /dev/null +++ b/.changes/unreleased/Security-20240906-215624.yaml @@ -0,0 +1,3 @@ +kind: Security +body: Updated hashicorp/setup-terraform action +time: 2024-09-06T21:56:24.0918138+02:00 diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 0c8a83d..190ace9 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -49,7 +49,7 @@ jobs: cache: true - name: Set up Terraform - uses: hashicorp/setup-terraform@651471c36a6092792c552e8b1bef71e592b462d8 # v3.1.1 + uses: hashicorp/setup-terraform@b9cd54a3c349d3f38e8881555d616ced269862dd # v3.1.2 with: terraform_version: '1.9.4' terraform_wrapper: false @@ -101,7 +101,7 @@ jobs: cache: true - name: Set up Terraform - uses: hashicorp/setup-terraform@651471c36a6092792c552e8b1bef71e592b462d8 # v3.1.1 + uses: hashicorp/setup-terraform@b9cd54a3c349d3f38e8881555d616ced269862dd # v3.1.2 with: terraform_version: ${{ matrix.terraform }} terraform_wrapper: false From 0c4f22c03c0972b753855be02ae7f8d91a85200f Mon Sep 17 00:00:00 2001 From: Dmitry Starov Date: Fri, 6 Sep 2024 22:05:30 +0200 Subject: [PATCH 02/11] Bump hashicorp/terraform-plugin-testing --- .../unreleased/Security-20240906-220413.yaml | 3 ++ go.mod | 17 +++++----- go.sum | 34 ++++++++++--------- tools/go.mod | 6 ++-- tools/go.sum | 3 ++ 5 files changed, 36 insertions(+), 27 deletions(-) create mode 100644 .changes/unreleased/Security-20240906-220413.yaml diff --git a/.changes/unreleased/Security-20240906-220413.yaml b/.changes/unreleased/Security-20240906-220413.yaml new file mode 100644 index 0000000..0cc5c5e --- /dev/null +++ b/.changes/unreleased/Security-20240906-220413.yaml @@ -0,0 +1,3 @@ +kind: Security +body: Bumped hashicorp/terraform-plugin-testing +time: 2024-09-06T22:04:13.773338+02:00 diff --git a/go.mod b/go.mod index e74db35..272bc65 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +7,7 @@ require ( github.com/hashicorp/terraform-plugin-framework-validators v0.13.0 github.com/hashicorp/terraform-plugin-go v0.23.0 github.com/hashicorp/terraform-plugin-log v0.9.0 - github.com/hashicorp/terraform-plugin-testing v1.9.0 + github.com/hashicorp/terraform-plugin-testing v1.10.0 ) require ( @@ -25,9 +25,10 @@ require ( github.com/hashicorp/go-hclog v1.6.3 // indirect github.com/hashicorp/go-multierror v1.1.1 // indirect github.com/hashicorp/go-plugin v1.6.0 // indirect + github.com/hashicorp/go-retryablehttp v0.7.7 // indirect github.com/hashicorp/go-uuid v1.0.3 // indirect github.com/hashicorp/go-version v1.7.0 // indirect - github.com/hashicorp/hc-install v0.7.0 // indirect + github.com/hashicorp/hc-install v0.8.0 // indirect github.com/hashicorp/hcl/v2 v2.21.0 // indirect github.com/hashicorp/logutils v1.0.0 // indirect github.com/hashicorp/terraform-exec v0.21.0 // indirect @@ -50,13 +51,13 @@ require ( github.com/vmihailenco/msgpack v4.0.4+incompatible // indirect github.com/vmihailenco/msgpack/v5 v5.4.1 // indirect github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect - github.com/zclconf/go-cty v1.14.4 // indirect - golang.org/x/crypto v0.25.0 // indirect - golang.org/x/mod v0.17.0 // indirect + github.com/zclconf/go-cty v1.15.0 // indirect + golang.org/x/crypto v0.26.0 // indirect + golang.org/x/mod v0.19.0 // indirect golang.org/x/net v0.25.0 // indirect - golang.org/x/sync v0.7.0 // indirect - golang.org/x/sys v0.22.0 // indirect - golang.org/x/text v0.16.0 // indirect + golang.org/x/sync v0.8.0 // indirect + golang.org/x/sys v0.23.0 // indirect + golang.org/x/text v0.17.0 // indirect golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect google.golang.org/appengine v1.6.8 // indirect google.golang.org/genproto/googleapis/rpc v0.0.0-20240227224415-6ceb2ff114de // indirect diff --git a/go.sum b/go.sum index 853618a..17d32f1 100644 --- a/go.sum +++ b/go.sum @@ -59,13 +59,15 @@ github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+l github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM= github.com/hashicorp/go-plugin v1.6.0 h1:wgd4KxHJTVGGqWBq4QPB1i5BZNEx9BR8+OFmHDmTk8A= github.com/hashicorp/go-plugin v1.6.0/go.mod h1:lBS5MtSSBZk0SHc66KACcjjlU6WzEVP/8pwz68aMkCI= +github.com/hashicorp/go-retryablehttp v0.7.7 h1:C8hUCYzor8PIfXHa4UrZkU4VvK8o9ISHxT2Q8+VepXU= +github.com/hashicorp/go-retryablehttp v0.7.7/go.mod h1:pkQpWZeYWskR+D1tR2O5OcBFOxfA7DoAO6xtkuQnHTk= github.com/hashicorp/go-uuid v1.0.0/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro= github.com/hashicorp/go-uuid v1.0.3 h1:2gKiV6YVmrJ1i2CKKa9obLvRieoRGviZFL26PcT/Co8= github.com/hashicorp/go-uuid v1.0.3/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro= github.com/hashicorp/go-version v1.7.0 h1:5tqGy27NaOTB8yJKUZELlFAS/LTKJkrmONwQKeRZfjY= github.com/hashicorp/go-version v1.7.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA= -github.com/hashicorp/hc-install v0.7.0 h1:Uu9edVqjKQxxuD28mR5TikkKDd/p55S8vzPC1659aBk= -github.com/hashicorp/hc-install v0.7.0/go.mod h1:ELmmzZlGnEcqoUMKUuykHaPCIR1sYLYX+KSggWSKZuA= +github.com/hashicorp/hc-install v0.8.0 h1:LdpZeXkZYMQhoKPCecJHlKvUkQFixN/nvyR1CdfOLjI= +github.com/hashicorp/hc-install v0.8.0/go.mod h1:+MwJYjDfCruSD/udvBmRB22Nlkwwkwf5sAB6uTIhSaU= github.com/hashicorp/hcl/v2 v2.21.0 h1:lve4q/o/2rqwYOgUg3y3V2YPyD1/zkCLGjIV74Jit14= github.com/hashicorp/hcl/v2 v2.21.0/go.mod h1:62ZYHrXgPoX8xBnzl8QzbWq4dyDsDtfCRgIq1rbJEvA= github.com/hashicorp/logutils v1.0.0 h1:dLEQVugN8vlakKOUE3ihGLTZJRB4j+M2cdTm/ORI65Y= @@ -84,8 +86,8 @@ github.com/hashicorp/terraform-plugin-log v0.9.0 h1:i7hOA+vdAItN1/7UrfBqBwvYPQ9T github.com/hashicorp/terraform-plugin-log v0.9.0/go.mod h1:rKL8egZQ/eXSyDqzLUuwUYLVdlYeamldAHSxjUFADow= github.com/hashicorp/terraform-plugin-sdk/v2 v2.34.0 h1:kJiWGx2kiQVo97Y5IOGR4EMcZ8DtMswHhUuFibsCQQE= github.com/hashicorp/terraform-plugin-sdk/v2 v2.34.0/go.mod h1:sl/UoabMc37HA6ICVMmGO+/0wofkVIRxf+BMb/dnoIg= -github.com/hashicorp/terraform-plugin-testing v1.9.0 h1:xOsQRqqlHKXpFq6etTxih3ubdK3HVDtfE1IY7Rpd37o= -github.com/hashicorp/terraform-plugin-testing v1.9.0/go.mod h1:fhhVx/8+XNJZTD5o3b4stfZ6+q7z9+lIWigIYdT6/44= +github.com/hashicorp/terraform-plugin-testing v1.10.0 h1:2+tmRNhvnfE4Bs8rB6v58S/VpqzGC6RCh9Y8ujdn+aw= +github.com/hashicorp/terraform-plugin-testing v1.10.0/go.mod h1:iWRW3+loP33WMch2P/TEyCxxct/ZEcCGMquSLSCVsrc= github.com/hashicorp/terraform-registry-address v0.2.3 h1:2TAiKJ1A3MAkZlH1YI/aTVcLZRu7JseiXNRHbOAyoTI= github.com/hashicorp/terraform-registry-address v0.2.3/go.mod h1:lFHA76T8jfQteVfT7caREqguFrW3c4MFSPhZB7HHgUM= github.com/hashicorp/terraform-svchost v0.1.1 h1:EZZimZ1GxdqFRinZ1tpJwVxxt49xc/S52uzrw4x0jKQ= @@ -155,17 +157,17 @@ github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV github.com/xanzy/ssh-agent v0.3.3 h1:+/15pJfg/RsTxqYcX6fHqOXZwwMP+2VyYWJeWM2qQFM= github.com/xanzy/ssh-agent v0.3.3/go.mod h1:6dzNDKs0J9rVPHPhaGCukekBHKqfl+L3KghI1Bc68Uw= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= -github.com/zclconf/go-cty v1.14.4 h1:uXXczd9QDGsgu0i/QFR/hzI5NYCHLf6NQw/atrbnhq8= -github.com/zclconf/go-cty v1.14.4/go.mod h1:VvMs5i0vgZdhYawQNq5kePSpLAoz8u1xvZgrPIxfnZE= +github.com/zclconf/go-cty v1.15.0 h1:tTCRWxsexYUmtt/wVxgDClUe+uQusuI443uL6e+5sXQ= +github.com/zclconf/go-cty v1.15.0/go.mod h1:VvMs5i0vgZdhYawQNq5kePSpLAoz8u1xvZgrPIxfnZE= github.com/zclconf/go-cty-debug v0.0.0-20240509010212-0d6042c53940 h1:4r45xpDWB6ZMSMNJFMOjqrGHynW3DIBuR2H9j0ug+Mo= github.com/zclconf/go-cty-debug v0.0.0-20240509010212-0d6042c53940/go.mod h1:CmBdvvj3nqzfzJ6nTCIwDTPZ56aVGvDrmztiO5g3qrM= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= -golang.org/x/crypto v0.25.0 h1:ypSNr+bnYL2YhwoMt2zPxHFmbAN1KZs/njMG3hxUp30= -golang.org/x/crypto v0.25.0/go.mod h1:T+wALwcMOSE0kXgUAnPAHqTLW+XHgcELELW8VaDgm/M= +golang.org/x/crypto v0.26.0 h1:RrRspgV4mU+YwB4FYnuBoKsUapNIL5cohGAmSH3azsw= +golang.org/x/crypto v0.26.0/go.mod h1:GY7jblb9wI+FOo5y8/S2oY4zWP07AkOJ4+jxCqdqn54= golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= -golang.org/x/mod v0.17.0 h1:zY54UmvipHiNd+pm+m0x9KhZ9hl1/7QNMyxXbc6ICqA= -golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= +golang.org/x/mod v0.19.0 h1:fEdghXQSo20giMthA7cd28ZC+jts4amQ3YMXiP5oMQ8= +golang.org/x/mod v0.19.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= @@ -175,8 +177,8 @@ golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M= -golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= +golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ= +golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -189,8 +191,8 @@ golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI= -golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.23.0 h1:YfKFowiIMvtgl1UERQoTPPToxltDeZfbj4H7dVUCwmM= +golang.org/x/sys v0.23.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= @@ -198,8 +200,8 @@ golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ= -golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4= -golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI= +golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc= +golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= diff --git a/tools/go.mod b/tools/go.mod index a115d4f..5a703e3 100644 --- a/tools/go.mod +++ b/tools/go.mod @@ -42,11 +42,11 @@ require ( github.com/yuin/goldmark-meta v1.1.0 // indirect github.com/zclconf/go-cty v1.14.4 // indirect go.abhg.dev/goldmark/frontmatter v0.2.0 // indirect - golang.org/x/crypto v0.21.0 // indirect + golang.org/x/crypto v0.23.0 // indirect golang.org/x/exp v0.0.0-20230626212559-97b1e661b5df // indirect golang.org/x/mod v0.17.0 // indirect - golang.org/x/sys v0.18.0 // indirect + golang.org/x/sys v0.20.0 // indirect golang.org/x/text v0.15.0 // indirect - gopkg.in/yaml.v2 v2.3.0 // indirect + gopkg.in/yaml.v2 v2.4.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/tools/go.sum b/tools/go.sum index 473f90a..047be61 100644 --- a/tools/go.sum +++ b/tools/go.sum @@ -140,6 +140,7 @@ golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5y golang.org/x/crypto v0.3.0/go.mod h1:hebNnKkNXi2UzZN1eVRvBB7co0a+JxK6XbPiWVs/3J4= golang.org/x/crypto v0.21.0 h1:X31++rzVUdKhX5sWmSOFZxx8UW/ldWx55cbf08iNAMA= golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs= +golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8= golang.org/x/exp v0.0.0-20230626212559-97b1e661b5df h1:UA2aFVmmsIlefxMk29Dp2juaUSth8Pyn3Tq5Y5mJGME= golang.org/x/exp v0.0.0-20230626212559-97b1e661b5df/go.mod h1:FXUEEKJgO7OQYeo8N01OfiKP8RXMtf6e8aTskBGqWdc= golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= @@ -163,6 +164,7 @@ golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4= golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/term v0.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc= @@ -185,5 +187,6 @@ gopkg.in/warnings.v0 v0.1.2/go.mod h1:jksf8JmL6Qr/oQM2OXTHunEvvTAsrWBLb6OOjuVWRN gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU= gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= From 7f5f7281beecf535413bc3feaa6d523fda97d87a Mon Sep 17 00:00:00 2001 From: Dmitry Starov Date: Sat, 14 Sep 2024 22:12:28 +0200 Subject: [PATCH 03/11] Bump version to `v0.1.1` --- .changes/unreleased/Security-20240906-215624.yaml | 3 --- .changes/unreleased/Security-20240906-220413.yaml | 3 --- .changes/v0.1.0.md | 2 ++ .changes/v0.1.1.md | 6 ++++++ .changie.yaml | 11 ++++++----- CHANGELOG.md | 12 ++++++++++++ 6 files changed, 26 insertions(+), 11 deletions(-) delete mode 100644 .changes/unreleased/Security-20240906-215624.yaml delete mode 100644 .changes/unreleased/Security-20240906-220413.yaml create mode 100644 .changes/v0.1.1.md diff --git a/.changes/unreleased/Security-20240906-215624.yaml b/.changes/unreleased/Security-20240906-215624.yaml deleted file mode 100644 index 54c190c..0000000 --- a/.changes/unreleased/Security-20240906-215624.yaml +++ /dev/null @@ -1,3 +0,0 @@ -kind: Security -body: Updated hashicorp/setup-terraform action -time: 2024-09-06T21:56:24.0918138+02:00 diff --git a/.changes/unreleased/Security-20240906-220413.yaml b/.changes/unreleased/Security-20240906-220413.yaml deleted file mode 100644 index 0cc5c5e..0000000 --- a/.changes/unreleased/Security-20240906-220413.yaml +++ /dev/null @@ -1,3 +0,0 @@ -kind: Security -body: Bumped hashicorp/terraform-plugin-testing -time: 2024-09-06T22:04:13.773338+02:00 diff --git a/.changes/v0.1.0.md b/.changes/v0.1.0.md index be69f1a..9eac4bc 100644 --- a/.changes/v0.1.0.md +++ b/.changes/v0.1.0.md @@ -1,3 +1,5 @@ ## v0.1.0 - 2024-08-08 + ### Added + * data-source/data_size: Implemented data source for conversion of data size units. diff --git a/.changes/v0.1.1.md b/.changes/v0.1.1.md new file mode 100644 index 0000000..e5b1152 --- /dev/null +++ b/.changes/v0.1.1.md @@ -0,0 +1,6 @@ +## v0.1.1 - 2024-09-14 + +### Security + +* Updated hashicorp/setup-terraform action +* Bumped hashicorp/terraform-plugin-testing diff --git a/.changie.yaml b/.changie.yaml index 9543dd1..d17af70 100644 --- a/.changie.yaml +++ b/.changie.yaml @@ -7,15 +7,15 @@ kindFormat: '### {{.Kind}}' changeFormat: '* {{.Body}}' kinds: - label: Added - auto: minor + auto: major - label: Changed auto: major - - label: Enhanced - auto: minor - - label: Deprecated - auto: minor - label: Removed auto: major + - label: Deprecated + auto: minor + - label: Enhanced + auto: minor - label: Fixed auto: patch - label: Security @@ -23,4 +23,5 @@ kinds: newlines: afterKind: 1 beforeKind: 1 + afterChangelogVersion: 2 endOfVersion: 1 diff --git a/CHANGELOG.md b/CHANGELOG.md index be69f1a..5682ac9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,15 @@ +## v0.1.1 - 2024-09-14 + +### Security + +* Updated hashicorp/setup-terraform action +* Bumped hashicorp/terraform-plugin-testing + + ## v0.1.0 - 2024-08-08 + ### Added + * data-source/data_size: Implemented data source for conversion of data size units. + + From 345afd7d4026697d829d39b7ea0b6beaf0ca8fb7 Mon Sep 17 00:00:00 2001 From: Dmitry Starov Date: Sat, 14 Sep 2024 18:30:55 +0200 Subject: [PATCH 04/11] Remove a little of boilerplate in data size data source --- .../unreleased/Enhanced-20240914-182812.yaml | 3 + internal/converter/converter.go | 6 +- internal/converter/converter_data_size.go | 26 ++++- internal/provider/datasource/data_size.go | 94 +++++-------------- .../provider/datasource/data_size_test.go | 6 +- 5 files changed, 55 insertions(+), 80 deletions(-) create mode 100644 .changes/unreleased/Enhanced-20240914-182812.yaml diff --git a/.changes/unreleased/Enhanced-20240914-182812.yaml b/.changes/unreleased/Enhanced-20240914-182812.yaml new file mode 100644 index 0000000..6e9b1c2 --- /dev/null +++ b/.changes/unreleased/Enhanced-20240914-182812.yaml @@ -0,0 +1,3 @@ +kind: Enhanced +body: Removed a little of boilerplate in data size data source +time: 2024-09-14T18:28:12.6612619+02:00 diff --git a/internal/converter/converter.go b/internal/converter/converter.go index f9b503a..ee3a910 100644 --- a/internal/converter/converter.go +++ b/internal/converter/converter.go @@ -1,5 +1,7 @@ -// Copyright (c) Dmitrii Starov -// SPDX-License-Identifier: MPL-2.0 +/* + * Copyright (c) 2024. Dmitry Starov + * SPDX-License-Identifier: MPL-2.0 + */ package converter diff --git a/internal/converter/converter_data_size.go b/internal/converter/converter_data_size.go index cac1654..e5cc913 100644 --- a/internal/converter/converter_data_size.go +++ b/internal/converter/converter_data_size.go @@ -1,5 +1,7 @@ -// Copyright (c) Dmitrii Starov -// SPDX-License-Identifier: MPL-2.0 +/* + * Copyright (c) 2024. Dmitry Starov + * SPDX-License-Identifier: MPL-2.0 + */ package converter @@ -30,9 +32,9 @@ var ( Peta = new(big.Float).Mul(Tera, Kilo) ) -type DataSizeConverter func(types.Number) types.Number +type dataSizeConverter func(types.Number) types.Number -func bytesTo(coefficient *big.Float) DataSizeConverter { +func bytesTo(coefficient *big.Float) dataSizeConverter { return func(number types.Number) types.Number { if number.Equal(zero) { return types.NumberValue(number.ValueBigFloat()) @@ -47,7 +49,7 @@ func bytesTo(coefficient *big.Float) DataSizeConverter { } } -func toBytes(coefficient *big.Float) DataSizeConverter { +func toBytes(coefficient *big.Float) dataSizeConverter { return func(number types.Number) types.Number { if number.Equal(zero) { return types.NumberValue(number.ValueBigFloat()) @@ -87,3 +89,17 @@ var ( TerabytesToBytes = toBytes(Tera) PetabytesToBytes = toBytes(Peta) ) + +var DataSizeNames = []string{ + "bytes", + "kibibytes", + "mebibytes", + "gibibytes", + "tebibytes", + "pebibytes", + "kilobytes", + "megabytes", + "gigabytes", + "terabytes", + "petabytes", +} diff --git a/internal/provider/datasource/data_size.go b/internal/provider/datasource/data_size.go index 141f356..fb4ca03 100644 --- a/internal/provider/datasource/data_size.go +++ b/internal/provider/datasource/data_size.go @@ -1,10 +1,13 @@ -// Copyright (c) Dmitrii Starov -// SPDX-License-Identifier: MPL-2.0 +/* + * Copyright (c) 2024. Dmitry Starov + * SPDX-License-Identifier: MPL-2.0 + */ package datasource import ( "context" + "fmt" "math/big" "strings" @@ -113,67 +116,21 @@ func (d *DataSize) Metadata(_ context.Context, req datasource.MetadataRequest, r } func (d *DataSize) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { + attributes := map[string]schema.Attribute{} + for _, dataSizeName := range converter.DataSizeNames { + description := fmt.Sprintf("Data size in %s.", dataSizeName) + attributes[dataSizeName] = schema.NumberAttribute{ + Description: description, + MarkdownDescription: description, + Optional: true, + Computed: true, + } + } + resp.Schema = schema.Schema{ Description: dataSizeDescription, MarkdownDescription: dataSizeDescriptionMd, - - Attributes: map[string]schema.Attribute{ - "bytes": schema.NumberAttribute{ - MarkdownDescription: "Data size in bytes.", - Optional: true, - Computed: true, - }, - "kibibytes": schema.NumberAttribute{ - MarkdownDescription: "Data size in kibibytes.", - Optional: true, - Computed: true, - }, - "mebibytes": schema.NumberAttribute{ - MarkdownDescription: "Data size in mebibytes.", - Optional: true, - Computed: true, - }, - "gibibytes": schema.NumberAttribute{ - MarkdownDescription: "Data size in gibibytes.", - Optional: true, - Computed: true, - }, - "tebibytes": schema.NumberAttribute{ - MarkdownDescription: "Data size in tebibytes.", - Optional: true, - Computed: true, - }, - "pebibytes": schema.NumberAttribute{ - MarkdownDescription: "Data size in pebibytes.", - Optional: true, - Computed: true, - }, - "kilobytes": schema.NumberAttribute{ - MarkdownDescription: "Data size in kilobytes.", - Optional: true, - Computed: true, - }, - "megabytes": schema.NumberAttribute{ - MarkdownDescription: "Data size in megabytes.", - Optional: true, - Computed: true, - }, - "gigabytes": schema.NumberAttribute{ - MarkdownDescription: "Data size in gigabytes.", - Optional: true, - Computed: true, - }, - "terabytes": schema.NumberAttribute{ - MarkdownDescription: "Data size in terabytes.", - Optional: true, - Computed: true, - }, - "petabytes": schema.NumberAttribute{ - MarkdownDescription: "Data size in petabytes.", - Optional: true, - Computed: true, - }, - }, + Attributes: attributes, } } @@ -192,19 +149,14 @@ func (d *DataSize) Read(ctx context.Context, req datasource.ReadRequest, resp *d } func (d *DataSize) ConfigValidators(_ context.Context) []datasource.ConfigValidator { + var expressions []path.Expression + for _, dataSizeName := range converter.DataSizeNames { + expressions = append(expressions, path.MatchRoot(dataSizeName)) + } + return []datasource.ConfigValidator{ datasourcevalidator.ExactlyOneOf( - path.MatchRoot("bytes"), - path.MatchRoot("kibibytes"), - path.MatchRoot("mebibytes"), - path.MatchRoot("gibibytes"), - path.MatchRoot("tebibytes"), - path.MatchRoot("pebibytes"), - path.MatchRoot("kilobytes"), - path.MatchRoot("megabytes"), - path.MatchRoot("gigabytes"), - path.MatchRoot("terabytes"), - path.MatchRoot("petabytes"), + expressions..., ), } } diff --git a/internal/provider/datasource/data_size_test.go b/internal/provider/datasource/data_size_test.go index 984e816..841d959 100644 --- a/internal/provider/datasource/data_size_test.go +++ b/internal/provider/datasource/data_size_test.go @@ -1,5 +1,7 @@ -// Copyright (c) Dmitrii Starov -// SPDX-License-Identifier: MPL-2.0 +/* + * Copyright (c) 2024. Dmitry Starov + * SPDX-License-Identifier: MPL-2.0 + */ package datasource_test From cb71bc0c2e43dff94444a275f30c756b87cfc383 Mon Sep 17 00:00:00 2001 From: Dmitry Starov Date: Sun, 15 Sep 2024 01:12:06 +0200 Subject: [PATCH 05/11] Bump version to `v0.2.0` --- .changes/unreleased/Enhanced-20240914-182812.yaml | 3 --- .changes/v0.2.0.md | 5 +++++ CHANGELOG.md | 7 +++++++ 3 files changed, 12 insertions(+), 3 deletions(-) delete mode 100644 .changes/unreleased/Enhanced-20240914-182812.yaml create mode 100644 .changes/v0.2.0.md diff --git a/.changes/unreleased/Enhanced-20240914-182812.yaml b/.changes/unreleased/Enhanced-20240914-182812.yaml deleted file mode 100644 index 6e9b1c2..0000000 --- a/.changes/unreleased/Enhanced-20240914-182812.yaml +++ /dev/null @@ -1,3 +0,0 @@ -kind: Enhanced -body: Removed a little of boilerplate in data size data source -time: 2024-09-14T18:28:12.6612619+02:00 diff --git a/.changes/v0.2.0.md b/.changes/v0.2.0.md new file mode 100644 index 0000000..434863e --- /dev/null +++ b/.changes/v0.2.0.md @@ -0,0 +1,5 @@ +## v0.2.0 - 2024-09-15 + +### Enhanced + +* Removed a little of boilerplate in data size data source diff --git a/CHANGELOG.md b/CHANGELOG.md index 5682ac9..164b8f0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +## v0.2.0 - 2024-09-15 + +### Enhanced + +* Removed a little of boilerplate in data size data source + + ## v0.1.1 - 2024-09-14 ### Security From 9816e68e823be561674fc65a00b7398bdbea1772 Mon Sep 17 00:00:00 2001 From: Dmitry Starov Date: Sun, 15 Sep 2024 02:58:35 +0200 Subject: [PATCH 06/11] Update dependency versions in tools --- .changes/unreleased/Security-20240915-025743.yaml | 3 +++ tools/go.sum | 8 +++----- 2 files changed, 6 insertions(+), 5 deletions(-) create mode 100644 .changes/unreleased/Security-20240915-025743.yaml diff --git a/.changes/unreleased/Security-20240915-025743.yaml b/.changes/unreleased/Security-20240915-025743.yaml new file mode 100644 index 0000000..6ca7530 --- /dev/null +++ b/.changes/unreleased/Security-20240915-025743.yaml @@ -0,0 +1,3 @@ +kind: Security +body: Updated dependency versions for doc generator. +time: 2024-09-15T02:57:43.8201012+02:00 diff --git a/tools/go.sum b/tools/go.sum index 047be61..6e60386 100644 --- a/tools/go.sum +++ b/tools/go.sum @@ -138,8 +138,7 @@ go.abhg.dev/goldmark/frontmatter v0.2.0/go.mod h1:XqrEkZuM57djk7zrlRUB02x8I5J0px golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/crypto v0.3.0/go.mod h1:hebNnKkNXi2UzZN1eVRvBB7co0a+JxK6XbPiWVs/3J4= -golang.org/x/crypto v0.21.0 h1:X31++rzVUdKhX5sWmSOFZxx8UW/ldWx55cbf08iNAMA= -golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs= +golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI= golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8= golang.org/x/exp v0.0.0-20230626212559-97b1e661b5df h1:UA2aFVmmsIlefxMk29Dp2juaUSth8Pyn3Tq5Y5mJGME= golang.org/x/exp v0.0.0-20230626212559-97b1e661b5df/go.mod h1:FXUEEKJgO7OQYeo8N01OfiKP8RXMtf6e8aTskBGqWdc= @@ -162,8 +161,7 @@ golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4= -golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y= golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= @@ -185,8 +183,8 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8 gopkg.in/warnings.v0 v0.1.2 h1:wFXVbFY8DY5/xOe1ECiWdKCzZlxgshcYVNkBHstARME= gopkg.in/warnings.v0 v0.1.2/go.mod h1:jksf8JmL6Qr/oQM2OXTHunEvvTAsrWBLb6OOjuVWRNI= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU= gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= From 9800bea65f6a3931ac696ab768ec2cfb798af82b Mon Sep 17 00:00:00 2001 From: Dmitry Starov Date: Sun, 15 Sep 2024 02:59:38 +0200 Subject: [PATCH 07/11] Add EditorConfig --- .../unreleased/Enhanced-20240915-025916.yaml | 3 +++ .editorconfig | 17 +++++++++++++++++ .github/.editorconfig | 2 ++ 3 files changed, 22 insertions(+) create mode 100644 .changes/unreleased/Enhanced-20240915-025916.yaml create mode 100644 .editorconfig create mode 100644 .github/.editorconfig diff --git a/.changes/unreleased/Enhanced-20240915-025916.yaml b/.changes/unreleased/Enhanced-20240915-025916.yaml new file mode 100644 index 0000000..04cbe13 --- /dev/null +++ b/.changes/unreleased/Enhanced-20240915-025916.yaml @@ -0,0 +1,3 @@ +kind: Enhanced +body: Added EditorConfig. +time: 2024-09-15T02:59:16.5811877+02:00 diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..54f68e9 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,17 @@ +root = true + +[*] +charset = utf-8 + +end_of_line = LF +insert_final_newline = true +trim_trailing_whitespace = true + +indent_style = space +indent_size = 4 + +[{GNUmakefile,Makefile,makefile}] +indent_style = tab + +[{*.go,*.go.gotmpl}] +indent_style = tab diff --git a/.github/.editorconfig b/.github/.editorconfig new file mode 100644 index 0000000..0902c6a --- /dev/null +++ b/.github/.editorconfig @@ -0,0 +1,2 @@ +[{*.yml,*.yaml}] +indent_size = 2 From ada50e3e063c86085d075dc25bcf4f4cac71fecf Mon Sep 17 00:00:00 2001 From: Dmitry Starov Date: Sun, 15 Sep 2024 03:01:29 +0200 Subject: [PATCH 08/11] Fix configs of CI tools --- .changes/unreleased/Fixed-20240915-030024.yaml | 3 +++ .golangci.yml | 1 - .goreleaser.yml | 2 +- 3 files changed, 4 insertions(+), 2 deletions(-) create mode 100644 .changes/unreleased/Fixed-20240915-030024.yaml diff --git a/.changes/unreleased/Fixed-20240915-030024.yaml b/.changes/unreleased/Fixed-20240915-030024.yaml new file mode 100644 index 0000000..f64fead --- /dev/null +++ b/.changes/unreleased/Fixed-20240915-030024.yaml @@ -0,0 +1,3 @@ +kind: Fixed +body: CI tool configs. +time: 2024-09-15T03:00:24.8989296+02:00 diff --git a/.golangci.yml b/.golangci.yml index dd7637c..07ece92 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -7,7 +7,6 @@ linters: enable: - durationcheck - errcheck - - exportloopref - forcetypeassert - godot - gofmt diff --git a/.goreleaser.yml b/.goreleaser.yml index e0a531a..5f4ed05 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -9,7 +9,7 @@ builds: flags: - -trimpath ldflags: - - '-s -w -X main.version={{.Version}} -X main.commit={{.Commit}}' + - '-s -w -X main.version={{ .Version }} -X main.commit={{ .Commit }}' goos: - freebsd - windows From 53933c4f43eadcd27e6772c8fbae77903d7202bf Mon Sep 17 00:00:00 2001 From: Dmitry Starov Date: Sun, 15 Sep 2024 03:03:43 +0200 Subject: [PATCH 09/11] Implement provider-defined functions for data size conversion --- .../unreleased/Added-20240915-030145.yaml | 3 + GNUmakefile | 12 +- README.md | 42 ++-- docs/functions/from_gb.md | 32 +++ docs/functions/from_gib.md | 32 +++ docs/functions/from_kb.md | 32 +++ docs/functions/from_kib.md | 32 +++ docs/functions/from_mb.md | 32 +++ docs/functions/from_mib.md | 32 +++ docs/functions/from_pb.md | 32 +++ docs/functions/from_pib.md | 32 +++ docs/functions/from_tb.md | 32 +++ docs/functions/from_tib.md | 32 +++ docs/functions/to_gb.md | 32 +++ docs/functions/to_gib.md | 32 +++ docs/functions/to_kb.md | 32 +++ docs/functions/to_kib.md | 32 +++ docs/functions/to_mb.md | 32 +++ docs/functions/to_mib.md | 32 +++ docs/functions/to_pb.md | 32 +++ docs/functions/to_pib.md | 32 +++ docs/functions/to_tb.md | 32 +++ docs/functions/to_tib.md | 32 +++ docs/index.md | 60 ++++-- examples/functions/from_gb/function.tf | 3 + examples/functions/from_gib/function.tf | 3 + examples/functions/from_kb/function.tf | 3 + examples/functions/from_kib/function.tf | 3 + examples/functions/from_mb/function.tf | 3 + examples/functions/from_mib/function.tf | 3 + examples/functions/from_pb/function.tf | 3 + examples/functions/from_pib/function.tf | 3 + examples/functions/from_tb/function.tf | 3 + examples/functions/from_tib/function.tf | 3 + examples/functions/to_gb/function.tf | 3 + examples/functions/to_gib/function.tf | 3 + examples/functions/to_kb/function.tf | 3 + examples/functions/to_kib/function.tf | 3 + examples/functions/to_mb/function.tf | 3 + examples/functions/to_mib/function.tf | 3 + examples/functions/to_pb/function.tf | 3 + examples/functions/to_pib/function.tf | 3 + examples/functions/to_tb/function.tf | 3 + examples/functions/to_tib/function.tf | 3 + go.mod | 1 + go.sum | 2 + internal/converter/converter_data_size.go | 20 +- internal/generator/datasize/main.go | 125 ++++++++++++ internal/generator/generator.go | 122 ++++++++++++ internal/generator/main.go | 31 +++ internal/generator/path.go | 49 +++++ .../templates/data_size_function.go.gotmpl | 68 +++++++ .../data_size_function_example.tf.gotmpl | 15 ++ .../templates/generated_functions.go.gotmpl | 28 +++ internal/provider/datasource/data_size.go | 22 +-- .../provider/datasource/data_size_test.go | 10 +- internal/provider/function/data_size_test.go | 185 ++++++++++++++++++ internal/provider/function/generated.go | 40 ++++ .../function/generated/data_size_gibibytes.go | 89 +++++++++ .../function/generated/data_size_gigabytes.go | 89 +++++++++ .../function/generated/data_size_kibibytes.go | 89 +++++++++ .../function/generated/data_size_kilobytes.go | 89 +++++++++ .../function/generated/data_size_mebibytes.go | 89 +++++++++ .../function/generated/data_size_megabytes.go | 89 +++++++++ .../function/generated/data_size_pebibytes.go | 89 +++++++++ .../function/generated/data_size_petabytes.go | 89 +++++++++ .../function/generated/data_size_tebibytes.go | 89 +++++++++ .../function/generated/data_size_terabytes.go | 89 +++++++++ internal/provider/provider.go | 49 +++-- 69 files changed, 2406 insertions(+), 68 deletions(-) create mode 100644 .changes/unreleased/Added-20240915-030145.yaml create mode 100644 docs/functions/from_gb.md create mode 100644 docs/functions/from_gib.md create mode 100644 docs/functions/from_kb.md create mode 100644 docs/functions/from_kib.md create mode 100644 docs/functions/from_mb.md create mode 100644 docs/functions/from_mib.md create mode 100644 docs/functions/from_pb.md create mode 100644 docs/functions/from_pib.md create mode 100644 docs/functions/from_tb.md create mode 100644 docs/functions/from_tib.md create mode 100644 docs/functions/to_gb.md create mode 100644 docs/functions/to_gib.md create mode 100644 docs/functions/to_kb.md create mode 100644 docs/functions/to_kib.md create mode 100644 docs/functions/to_mb.md create mode 100644 docs/functions/to_mib.md create mode 100644 docs/functions/to_pb.md create mode 100644 docs/functions/to_pib.md create mode 100644 docs/functions/to_tb.md create mode 100644 docs/functions/to_tib.md create mode 100644 examples/functions/from_gb/function.tf create mode 100644 examples/functions/from_gib/function.tf create mode 100644 examples/functions/from_kb/function.tf create mode 100644 examples/functions/from_kib/function.tf create mode 100644 examples/functions/from_mb/function.tf create mode 100644 examples/functions/from_mib/function.tf create mode 100644 examples/functions/from_pb/function.tf create mode 100644 examples/functions/from_pib/function.tf create mode 100644 examples/functions/from_tb/function.tf create mode 100644 examples/functions/from_tib/function.tf create mode 100644 examples/functions/to_gb/function.tf create mode 100644 examples/functions/to_gib/function.tf create mode 100644 examples/functions/to_kb/function.tf create mode 100644 examples/functions/to_kib/function.tf create mode 100644 examples/functions/to_mb/function.tf create mode 100644 examples/functions/to_mib/function.tf create mode 100644 examples/functions/to_pb/function.tf create mode 100644 examples/functions/to_pib/function.tf create mode 100644 examples/functions/to_tb/function.tf create mode 100644 examples/functions/to_tib/function.tf create mode 100644 internal/generator/datasize/main.go create mode 100644 internal/generator/generator.go create mode 100644 internal/generator/main.go create mode 100644 internal/generator/path.go create mode 100644 internal/generator/templates/data_size_function.go.gotmpl create mode 100644 internal/generator/templates/data_size_function_example.tf.gotmpl create mode 100644 internal/generator/templates/generated_functions.go.gotmpl create mode 100644 internal/provider/function/data_size_test.go create mode 100644 internal/provider/function/generated.go create mode 100644 internal/provider/function/generated/data_size_gibibytes.go create mode 100644 internal/provider/function/generated/data_size_gigabytes.go create mode 100644 internal/provider/function/generated/data_size_kibibytes.go create mode 100644 internal/provider/function/generated/data_size_kilobytes.go create mode 100644 internal/provider/function/generated/data_size_mebibytes.go create mode 100644 internal/provider/function/generated/data_size_megabytes.go create mode 100644 internal/provider/function/generated/data_size_pebibytes.go create mode 100644 internal/provider/function/generated/data_size_petabytes.go create mode 100644 internal/provider/function/generated/data_size_tebibytes.go create mode 100644 internal/provider/function/generated/data_size_terabytes.go diff --git a/.changes/unreleased/Added-20240915-030145.yaml b/.changes/unreleased/Added-20240915-030145.yaml new file mode 100644 index 0000000..102aa54 --- /dev/null +++ b/.changes/unreleased/Added-20240915-030145.yaml @@ -0,0 +1,3 @@ +kind: Added +body: Provider-defined functions to convert values without storing in the state. +time: 2024-09-15T03:01:45.6714822+02:00 diff --git a/GNUmakefile b/GNUmakefile index e7cd638..3fde3b0 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -19,7 +19,7 @@ lint: ## Run linting against Go code. ##@ Building .PHONY: build -build: ## Build provider. +build: generate ## Build provider. go build -v ./... .PHONY: install @@ -29,15 +29,19 @@ install: build ## Install provider. ##@ Testing .PHONY: test -test: ## Run unit-tests. +test: generate ## Run unit-tests. go test -v -cover -timeout=120s -parallel=6 ./... .PHONY:test-acceptance -test-acceptance: ## Run acceptance tests. +test-acceptance: generate ## Run acceptance tests. TF_ACC=1 go test -v -cover -timeout 120m ./... ##@ Misc .PHONY: generate -generate: ## Format Terraform files in examples and generate docs. +generate: ## Generate source files. + go generate ./internal/... + +.PHONY: generate-docs +generate-docs: ## Format Terraform files in examples and generate docs. cd tools && go generate ./... diff --git a/README.md b/README.md index c2fe8f3..be746c9 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,8 @@ # Units Terraform provider -This provider gives a possibility to use data sources as containers for measurement units and converting them in an interoperable manner. +This provider gives you a possibility to convert categorized units in an interoperable manner. +Use [data sources](https://developer.hashicorp.com/terraform/language/data-sources) as containers for measurement units and converting them. +Or, convert them using [provider-defined functions](https://www.hashicorp.com/blog/terraform-1-8-improves-extensibility-with-provider-defined-functions). ## Problem to solve @@ -10,38 +12,54 @@ This provider gives a possibility to use data sources as containers for measurem ```terraform resource "cloud_provider_disk" "this" { - size = var.disk_size_gib * 1024 * 1024 * 1024 + size = var.disk_size_gib * 1024 * 1024 * 1024 } - + resource "another_cloud_provider_disk" "that" { - size_gb = ceil((var.disk_size_gib * (1024 * 1024 * 1024)) / (1000 * 1000 * 1000)) + size_gb = ceil((var.disk_size_gib * (1024 * 1024 * 1024)) / (1000 * 1000 * 1000)) } ``` ## Solution -Simply use: +### Data source + +> With data sources, you can store converted values in a container, which will be stored in your state. ```terraform data "units_data_size" "disk" { -gibibytes = var.disk_size_gib + gibibytes = var.disk_size_gib +} + +resource "cloud_provider_disk" "this" { + size = data.units_data_size.disk.bytes +} + +resource "another_cloud_provider_disk" "that" { + size_gb = ceil(data.units_data_size.disk.gigabytes) } +``` +### Functions + +> Converter function results are being computed during `plan`, and won't be stored in the state. + +```terraform resource "cloud_provider_disk" "this" { -size = data.units_data_size.disk.bytes + size = provider::units::from_gib(var.disk_size_gib) } resource "another_cloud_provider_disk" "that" { -size_gb = ceil(data.units_data_size.disk.gigabytes) + size_gb = ceil(provider::units::to_gb(provider::units::from_gib(var.disk_size_gib))) } ``` ## Requirements -| Component | Version | -|:-----------------------------------------------------------------|:----------| -| [Terraform](https://developer.hashicorp.com/terraform/downloads) | `>= 1.0` | -| [Go](https://golang.org/doc/install) | `>= 1.21` | +| Component | Version | +|:-----------------------------------------------------------------|:-----------| +| [Terraform](https://developer.hashicorp.com/terraform/downloads) | `>= 1.8.0` | +| [Go](https://golang.org/doc/install) | `>= 1.21` | ## Liability diff --git a/docs/functions/from_gb.md b/docs/functions/from_gb.md new file mode 100644 index 0000000..60c3209 --- /dev/null +++ b/docs/functions/from_gb.md @@ -0,0 +1,32 @@ +--- +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "from_gb function - units" +subcategory: "" +description: |- + Converts gigabytes to bytes +--- + +# function: from_gb + +Given data size in **gigabytes**, converts it to **bytes**. + +## Example Usage + +```terraform +output "example" { + size_in_bytes = provider::units::from_gb(42) +} +``` + +## Signature + + +```text +from_gb(gigabytes number) number +``` + +## Arguments + + +1. `gigabytes` (Number) Data size in **gigabytes** + diff --git a/docs/functions/from_gib.md b/docs/functions/from_gib.md new file mode 100644 index 0000000..08db265 --- /dev/null +++ b/docs/functions/from_gib.md @@ -0,0 +1,32 @@ +--- +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "from_gib function - units" +subcategory: "" +description: |- + Converts gibibytes to bytes +--- + +# function: from_gib + +Given data size in **gibibytes**, converts it to **bytes**. + +## Example Usage + +```terraform +output "example" { + size_in_bytes = provider::units::from_gib(42) +} +``` + +## Signature + + +```text +from_gib(gibibytes number) number +``` + +## Arguments + + +1. `gibibytes` (Number) Data size in **gibibytes** + diff --git a/docs/functions/from_kb.md b/docs/functions/from_kb.md new file mode 100644 index 0000000..ecb307f --- /dev/null +++ b/docs/functions/from_kb.md @@ -0,0 +1,32 @@ +--- +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "from_kb function - units" +subcategory: "" +description: |- + Converts kilobytes to bytes +--- + +# function: from_kb + +Given data size in **kilobytes**, converts it to **bytes**. + +## Example Usage + +```terraform +output "example" { + size_in_bytes = provider::units::from_kb(42) +} +``` + +## Signature + + +```text +from_kb(kilobytes number) number +``` + +## Arguments + + +1. `kilobytes` (Number) Data size in **kilobytes** + diff --git a/docs/functions/from_kib.md b/docs/functions/from_kib.md new file mode 100644 index 0000000..575b72a --- /dev/null +++ b/docs/functions/from_kib.md @@ -0,0 +1,32 @@ +--- +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "from_kib function - units" +subcategory: "" +description: |- + Converts kibibytes to bytes +--- + +# function: from_kib + +Given data size in **kibibytes**, converts it to **bytes**. + +## Example Usage + +```terraform +output "example" { + size_in_bytes = provider::units::from_kib(42) +} +``` + +## Signature + + +```text +from_kib(kibibytes number) number +``` + +## Arguments + + +1. `kibibytes` (Number) Data size in **kibibytes** + diff --git a/docs/functions/from_mb.md b/docs/functions/from_mb.md new file mode 100644 index 0000000..7faa6db --- /dev/null +++ b/docs/functions/from_mb.md @@ -0,0 +1,32 @@ +--- +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "from_mb function - units" +subcategory: "" +description: |- + Converts megabytes to bytes +--- + +# function: from_mb + +Given data size in **megabytes**, converts it to **bytes**. + +## Example Usage + +```terraform +output "example" { + size_in_bytes = provider::units::from_mb(42) +} +``` + +## Signature + + +```text +from_mb(megabytes number) number +``` + +## Arguments + + +1. `megabytes` (Number) Data size in **megabytes** + diff --git a/docs/functions/from_mib.md b/docs/functions/from_mib.md new file mode 100644 index 0000000..d8450b5 --- /dev/null +++ b/docs/functions/from_mib.md @@ -0,0 +1,32 @@ +--- +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "from_mib function - units" +subcategory: "" +description: |- + Converts mebibytes to bytes +--- + +# function: from_mib + +Given data size in **mebibytes**, converts it to **bytes**. + +## Example Usage + +```terraform +output "example" { + size_in_bytes = provider::units::from_mib(42) +} +``` + +## Signature + + +```text +from_mib(mebibytes number) number +``` + +## Arguments + + +1. `mebibytes` (Number) Data size in **mebibytes** + diff --git a/docs/functions/from_pb.md b/docs/functions/from_pb.md new file mode 100644 index 0000000..6f9d493 --- /dev/null +++ b/docs/functions/from_pb.md @@ -0,0 +1,32 @@ +--- +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "from_pb function - units" +subcategory: "" +description: |- + Converts petabytes to bytes +--- + +# function: from_pb + +Given data size in **petabytes**, converts it to **bytes**. + +## Example Usage + +```terraform +output "example" { + size_in_bytes = provider::units::from_pb(42) +} +``` + +## Signature + + +```text +from_pb(petabytes number) number +``` + +## Arguments + + +1. `petabytes` (Number) Data size in **petabytes** + diff --git a/docs/functions/from_pib.md b/docs/functions/from_pib.md new file mode 100644 index 0000000..60b2b7f --- /dev/null +++ b/docs/functions/from_pib.md @@ -0,0 +1,32 @@ +--- +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "from_pib function - units" +subcategory: "" +description: |- + Converts pebibytes to bytes +--- + +# function: from_pib + +Given data size in **pebibytes**, converts it to **bytes**. + +## Example Usage + +```terraform +output "example" { + size_in_bytes = provider::units::from_pib(42) +} +``` + +## Signature + + +```text +from_pib(pebibytes number) number +``` + +## Arguments + + +1. `pebibytes` (Number) Data size in **pebibytes** + diff --git a/docs/functions/from_tb.md b/docs/functions/from_tb.md new file mode 100644 index 0000000..0c8a1e9 --- /dev/null +++ b/docs/functions/from_tb.md @@ -0,0 +1,32 @@ +--- +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "from_tb function - units" +subcategory: "" +description: |- + Converts terabytes to bytes +--- + +# function: from_tb + +Given data size in **terabytes**, converts it to **bytes**. + +## Example Usage + +```terraform +output "example" { + size_in_bytes = provider::units::from_tb(42) +} +``` + +## Signature + + +```text +from_tb(terabytes number) number +``` + +## Arguments + + +1. `terabytes` (Number) Data size in **terabytes** + diff --git a/docs/functions/from_tib.md b/docs/functions/from_tib.md new file mode 100644 index 0000000..322d6d6 --- /dev/null +++ b/docs/functions/from_tib.md @@ -0,0 +1,32 @@ +--- +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "from_tib function - units" +subcategory: "" +description: |- + Converts tebibytes to bytes +--- + +# function: from_tib + +Given data size in **tebibytes**, converts it to **bytes**. + +## Example Usage + +```terraform +output "example" { + size_in_bytes = provider::units::from_tib(42) +} +``` + +## Signature + + +```text +from_tib(tebibytes number) number +``` + +## Arguments + + +1. `tebibytes` (Number) Data size in **tebibytes** + diff --git a/docs/functions/to_gb.md b/docs/functions/to_gb.md new file mode 100644 index 0000000..2135d6b --- /dev/null +++ b/docs/functions/to_gb.md @@ -0,0 +1,32 @@ +--- +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "to_gb function - units" +subcategory: "" +description: |- + Converts bytes to gigabytes +--- + +# function: to_gb + +Given data size in **bytes**, converts it to **gigabytes**. + +## Example Usage + +```terraform +output "example" { + size_in_gigabytes = provider::units::to_gb(42) +} +``` + +## Signature + + +```text +to_gb(bytes number) number +``` + +## Arguments + + +1. `bytes` (Number) Data size in **bytes** + diff --git a/docs/functions/to_gib.md b/docs/functions/to_gib.md new file mode 100644 index 0000000..e11037e --- /dev/null +++ b/docs/functions/to_gib.md @@ -0,0 +1,32 @@ +--- +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "to_gib function - units" +subcategory: "" +description: |- + Converts bytes to gibibytes +--- + +# function: to_gib + +Given data size in **bytes**, converts it to **gibibytes**. + +## Example Usage + +```terraform +output "example" { + size_in_gibibytes = provider::units::to_gib(42) +} +``` + +## Signature + + +```text +to_gib(bytes number) number +``` + +## Arguments + + +1. `bytes` (Number) Data size in **bytes** + diff --git a/docs/functions/to_kb.md b/docs/functions/to_kb.md new file mode 100644 index 0000000..0548ffc --- /dev/null +++ b/docs/functions/to_kb.md @@ -0,0 +1,32 @@ +--- +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "to_kb function - units" +subcategory: "" +description: |- + Converts bytes to kilobytes +--- + +# function: to_kb + +Given data size in **bytes**, converts it to **kilobytes**. + +## Example Usage + +```terraform +output "example" { + size_in_kilobytes = provider::units::to_kb(42) +} +``` + +## Signature + + +```text +to_kb(bytes number) number +``` + +## Arguments + + +1. `bytes` (Number) Data size in **bytes** + diff --git a/docs/functions/to_kib.md b/docs/functions/to_kib.md new file mode 100644 index 0000000..ce78b96 --- /dev/null +++ b/docs/functions/to_kib.md @@ -0,0 +1,32 @@ +--- +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "to_kib function - units" +subcategory: "" +description: |- + Converts bytes to kibibytes +--- + +# function: to_kib + +Given data size in **bytes**, converts it to **kibibytes**. + +## Example Usage + +```terraform +output "example" { + size_in_kibibytes = provider::units::to_kib(42) +} +``` + +## Signature + + +```text +to_kib(bytes number) number +``` + +## Arguments + + +1. `bytes` (Number) Data size in **bytes** + diff --git a/docs/functions/to_mb.md b/docs/functions/to_mb.md new file mode 100644 index 0000000..946d3fc --- /dev/null +++ b/docs/functions/to_mb.md @@ -0,0 +1,32 @@ +--- +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "to_mb function - units" +subcategory: "" +description: |- + Converts bytes to megabytes +--- + +# function: to_mb + +Given data size in **bytes**, converts it to **megabytes**. + +## Example Usage + +```terraform +output "example" { + size_in_megabytes = provider::units::to_mb(42) +} +``` + +## Signature + + +```text +to_mb(bytes number) number +``` + +## Arguments + + +1. `bytes` (Number) Data size in **bytes** + diff --git a/docs/functions/to_mib.md b/docs/functions/to_mib.md new file mode 100644 index 0000000..3864db3 --- /dev/null +++ b/docs/functions/to_mib.md @@ -0,0 +1,32 @@ +--- +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "to_mib function - units" +subcategory: "" +description: |- + Converts bytes to mebibytes +--- + +# function: to_mib + +Given data size in **bytes**, converts it to **mebibytes**. + +## Example Usage + +```terraform +output "example" { + size_in_mebibytes = provider::units::to_mib(42) +} +``` + +## Signature + + +```text +to_mib(bytes number) number +``` + +## Arguments + + +1. `bytes` (Number) Data size in **bytes** + diff --git a/docs/functions/to_pb.md b/docs/functions/to_pb.md new file mode 100644 index 0000000..cfe4751 --- /dev/null +++ b/docs/functions/to_pb.md @@ -0,0 +1,32 @@ +--- +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "to_pb function - units" +subcategory: "" +description: |- + Converts bytes to petabytes +--- + +# function: to_pb + +Given data size in **bytes**, converts it to **petabytes**. + +## Example Usage + +```terraform +output "example" { + size_in_petabytes = provider::units::to_pb(42) +} +``` + +## Signature + + +```text +to_pb(bytes number) number +``` + +## Arguments + + +1. `bytes` (Number) Data size in **bytes** + diff --git a/docs/functions/to_pib.md b/docs/functions/to_pib.md new file mode 100644 index 0000000..42384df --- /dev/null +++ b/docs/functions/to_pib.md @@ -0,0 +1,32 @@ +--- +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "to_pib function - units" +subcategory: "" +description: |- + Converts bytes to pebibytes +--- + +# function: to_pib + +Given data size in **bytes**, converts it to **pebibytes**. + +## Example Usage + +```terraform +output "example" { + size_in_pebibytes = provider::units::to_pib(42) +} +``` + +## Signature + + +```text +to_pib(bytes number) number +``` + +## Arguments + + +1. `bytes` (Number) Data size in **bytes** + diff --git a/docs/functions/to_tb.md b/docs/functions/to_tb.md new file mode 100644 index 0000000..6dc832c --- /dev/null +++ b/docs/functions/to_tb.md @@ -0,0 +1,32 @@ +--- +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "to_tb function - units" +subcategory: "" +description: |- + Converts bytes to terabytes +--- + +# function: to_tb + +Given data size in **bytes**, converts it to **terabytes**. + +## Example Usage + +```terraform +output "example" { + size_in_terabytes = provider::units::to_tb(42) +} +``` + +## Signature + + +```text +to_tb(bytes number) number +``` + +## Arguments + + +1. `bytes` (Number) Data size in **bytes** + diff --git a/docs/functions/to_tib.md b/docs/functions/to_tib.md new file mode 100644 index 0000000..a2ac7cd --- /dev/null +++ b/docs/functions/to_tib.md @@ -0,0 +1,32 @@ +--- +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "to_tib function - units" +subcategory: "" +description: |- + Converts bytes to tebibytes +--- + +# function: to_tib + +Given data size in **bytes**, converts it to **tebibytes**. + +## Example Usage + +```terraform +output "example" { + size_in_tebibytes = provider::units::to_tib(42) +} +``` + +## Signature + + +```text +to_tib(bytes number) number +``` + +## Arguments + + +1. `bytes` (Number) Data size in **bytes** + diff --git a/docs/index.md b/docs/index.md index e9c4533..8e20636 100644 --- a/docs/index.md +++ b/docs/index.md @@ -3,31 +3,45 @@ page_title: "units Provider" subcategory: "" description: |- - This provider gives a possibility to use data sources as containers for measurement units and converting them in an interoperable manner. + This provider gives you a possibility to convert categorized units in an interoperable manner. + Use data sources https://developer.hashicorp.com/terraform/language/data-sources as containers for measurement units and converting them. + Or, convert them using provider-defined functions https://www.hashicorp.com/blog/terraform-1-8-improves-extensibility-with-provider-defined-functions. Problem to solve Tired of lacking possibility of an easy definition of quantities?One resource asks for disk size in GiB and other resource outputs it in MB?Tired of writing code like this? resource "cloud_provider_disk" "this" { - size = var.disk_size_gib * 1024 * 1024 * 1024 + size = var.disk_size_gib * 1024 * 1024 * 1024 } resource "another_cloud_provider_disk" "that" { - size_gb = ceil((var.disk_size_gib * (1024 * 1024 * 1024)) / (1000 * 1000 * 1000)) + size_gb = ceil((var.disk_size_gib * (1024 * 1024 * 1024)) / (1000 * 1000 * 1000)) } Solution - Simply use: + Data source + With data sources, you can store converted values in a container, which will be stored in your state. data "units_data_size" "disk" { - gibibytes = var.disk_size_gib + gibibytes = var.disk_size_gib } resource "cloud_provider_disk" "this" { - size = data.units_data_size.disk.bytes + size = data.units_data_size.disk.bytes } resource "another_cloud_provider_disk" "that" { - size_gb = ceil(data.units_data_size.disk.gigabytes) + size_gb = ceil(data.units_data_size.disk.gigabytes) + } + + Functions + Converter function results are being computed during plan, and won't be stored in the state. + + resource "cloud_provider_disk" "this" { + size = provider::units::from_gib(var.disk_size_gib) + } + + resource "another_cloud_provider_disk" "that" { + size_gb = ceil(provider::units::to_gb(provider::units::from_gib(var.disk_size_gib))) } Liability @@ -38,7 +52,9 @@ description: |- # units Provider -This provider gives a possibility to use data sources as containers for measurement units and converting them in an interoperable manner. +This provider gives you a possibility to convert categorized units in an interoperable manner. +Use [data sources](https://developer.hashicorp.com/terraform/language/data-sources) as containers for measurement units and converting them. +Or, convert them using [provider-defined functions](https://www.hashicorp.com/blog/terraform-1-8-improves-extensibility-with-provider-defined-functions). ## Problem to solve @@ -48,29 +64,45 @@ This provider gives a possibility to use data sources as containers for measurem ```terraform resource "cloud_provider_disk" "this" { - size = var.disk_size_gib * 1024 * 1024 * 1024 + size = var.disk_size_gib * 1024 * 1024 * 1024 } resource "another_cloud_provider_disk" "that" { - size_gb = ceil((var.disk_size_gib * (1024 * 1024 * 1024)) / (1000 * 1000 * 1000)) + size_gb = ceil((var.disk_size_gib * (1024 * 1024 * 1024)) / (1000 * 1000 * 1000)) } ``` ## Solution -Simply use: +### Data source + +> With data sources, you can store converted values in a container, which will be stored in your state. ```terraform data "units_data_size" "disk" { - gibibytes = var.disk_size_gib + gibibytes = var.disk_size_gib +} + +resource "cloud_provider_disk" "this" { + size = data.units_data_size.disk.bytes } +resource "another_cloud_provider_disk" "that" { + size_gb = ceil(data.units_data_size.disk.gigabytes) +} +``` + +### Functions + +> Converter function results are being computed during `plan`, and won't be stored in the state. + +```terraform resource "cloud_provider_disk" "this" { - size = data.units_data_size.disk.bytes + size = provider::units::from_gib(var.disk_size_gib) } resource "another_cloud_provider_disk" "that" { - size_gb = ceil(data.units_data_size.disk.gigabytes) + size_gb = ceil(provider::units::to_gb(provider::units::from_gib(var.disk_size_gib))) } ``` diff --git a/examples/functions/from_gb/function.tf b/examples/functions/from_gb/function.tf new file mode 100644 index 0000000..9b9e14a --- /dev/null +++ b/examples/functions/from_gb/function.tf @@ -0,0 +1,3 @@ +output "example" { + size_in_bytes = provider::units::from_gb(42) +} \ No newline at end of file diff --git a/examples/functions/from_gib/function.tf b/examples/functions/from_gib/function.tf new file mode 100644 index 0000000..9f43ec0 --- /dev/null +++ b/examples/functions/from_gib/function.tf @@ -0,0 +1,3 @@ +output "example" { + size_in_bytes = provider::units::from_gib(42) +} \ No newline at end of file diff --git a/examples/functions/from_kb/function.tf b/examples/functions/from_kb/function.tf new file mode 100644 index 0000000..987793a --- /dev/null +++ b/examples/functions/from_kb/function.tf @@ -0,0 +1,3 @@ +output "example" { + size_in_bytes = provider::units::from_kb(42) +} \ No newline at end of file diff --git a/examples/functions/from_kib/function.tf b/examples/functions/from_kib/function.tf new file mode 100644 index 0000000..9c37481 --- /dev/null +++ b/examples/functions/from_kib/function.tf @@ -0,0 +1,3 @@ +output "example" { + size_in_bytes = provider::units::from_kib(42) +} \ No newline at end of file diff --git a/examples/functions/from_mb/function.tf b/examples/functions/from_mb/function.tf new file mode 100644 index 0000000..3583a31 --- /dev/null +++ b/examples/functions/from_mb/function.tf @@ -0,0 +1,3 @@ +output "example" { + size_in_bytes = provider::units::from_mb(42) +} \ No newline at end of file diff --git a/examples/functions/from_mib/function.tf b/examples/functions/from_mib/function.tf new file mode 100644 index 0000000..16b7e84 --- /dev/null +++ b/examples/functions/from_mib/function.tf @@ -0,0 +1,3 @@ +output "example" { + size_in_bytes = provider::units::from_mib(42) +} \ No newline at end of file diff --git a/examples/functions/from_pb/function.tf b/examples/functions/from_pb/function.tf new file mode 100644 index 0000000..2948897 --- /dev/null +++ b/examples/functions/from_pb/function.tf @@ -0,0 +1,3 @@ +output "example" { + size_in_bytes = provider::units::from_pb(42) +} \ No newline at end of file diff --git a/examples/functions/from_pib/function.tf b/examples/functions/from_pib/function.tf new file mode 100644 index 0000000..d44664e --- /dev/null +++ b/examples/functions/from_pib/function.tf @@ -0,0 +1,3 @@ +output "example" { + size_in_bytes = provider::units::from_pib(42) +} \ No newline at end of file diff --git a/examples/functions/from_tb/function.tf b/examples/functions/from_tb/function.tf new file mode 100644 index 0000000..ec90319 --- /dev/null +++ b/examples/functions/from_tb/function.tf @@ -0,0 +1,3 @@ +output "example" { + size_in_bytes = provider::units::from_tb(42) +} \ No newline at end of file diff --git a/examples/functions/from_tib/function.tf b/examples/functions/from_tib/function.tf new file mode 100644 index 0000000..383668c --- /dev/null +++ b/examples/functions/from_tib/function.tf @@ -0,0 +1,3 @@ +output "example" { + size_in_bytes = provider::units::from_tib(42) +} \ No newline at end of file diff --git a/examples/functions/to_gb/function.tf b/examples/functions/to_gb/function.tf new file mode 100644 index 0000000..d1fa51e --- /dev/null +++ b/examples/functions/to_gb/function.tf @@ -0,0 +1,3 @@ +output "example" { + size_in_gigabytes = provider::units::to_gb(42) +} \ No newline at end of file diff --git a/examples/functions/to_gib/function.tf b/examples/functions/to_gib/function.tf new file mode 100644 index 0000000..1e51439 --- /dev/null +++ b/examples/functions/to_gib/function.tf @@ -0,0 +1,3 @@ +output "example" { + size_in_gibibytes = provider::units::to_gib(42) +} \ No newline at end of file diff --git a/examples/functions/to_kb/function.tf b/examples/functions/to_kb/function.tf new file mode 100644 index 0000000..2257f50 --- /dev/null +++ b/examples/functions/to_kb/function.tf @@ -0,0 +1,3 @@ +output "example" { + size_in_kilobytes = provider::units::to_kb(42) +} \ No newline at end of file diff --git a/examples/functions/to_kib/function.tf b/examples/functions/to_kib/function.tf new file mode 100644 index 0000000..52a0101 --- /dev/null +++ b/examples/functions/to_kib/function.tf @@ -0,0 +1,3 @@ +output "example" { + size_in_kibibytes = provider::units::to_kib(42) +} \ No newline at end of file diff --git a/examples/functions/to_mb/function.tf b/examples/functions/to_mb/function.tf new file mode 100644 index 0000000..93281eb --- /dev/null +++ b/examples/functions/to_mb/function.tf @@ -0,0 +1,3 @@ +output "example" { + size_in_megabytes = provider::units::to_mb(42) +} \ No newline at end of file diff --git a/examples/functions/to_mib/function.tf b/examples/functions/to_mib/function.tf new file mode 100644 index 0000000..16e5892 --- /dev/null +++ b/examples/functions/to_mib/function.tf @@ -0,0 +1,3 @@ +output "example" { + size_in_mebibytes = provider::units::to_mib(42) +} \ No newline at end of file diff --git a/examples/functions/to_pb/function.tf b/examples/functions/to_pb/function.tf new file mode 100644 index 0000000..623cdcf --- /dev/null +++ b/examples/functions/to_pb/function.tf @@ -0,0 +1,3 @@ +output "example" { + size_in_petabytes = provider::units::to_pb(42) +} \ No newline at end of file diff --git a/examples/functions/to_pib/function.tf b/examples/functions/to_pib/function.tf new file mode 100644 index 0000000..4b8eada --- /dev/null +++ b/examples/functions/to_pib/function.tf @@ -0,0 +1,3 @@ +output "example" { + size_in_pebibytes = provider::units::to_pib(42) +} \ No newline at end of file diff --git a/examples/functions/to_tb/function.tf b/examples/functions/to_tb/function.tf new file mode 100644 index 0000000..ef3cc09 --- /dev/null +++ b/examples/functions/to_tb/function.tf @@ -0,0 +1,3 @@ +output "example" { + size_in_terabytes = provider::units::to_tb(42) +} \ No newline at end of file diff --git a/examples/functions/to_tib/function.tf b/examples/functions/to_tib/function.tf new file mode 100644 index 0000000..7c75e3a --- /dev/null +++ b/examples/functions/to_tib/function.tf @@ -0,0 +1,3 @@ +output "example" { + size_in_tebibytes = provider::units::to_tib(42) +} \ No newline at end of file diff --git a/go.mod b/go.mod index 272bc65..f50464a 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/dstaroff/terraform-provider-units go 1.21 require ( + github.com/Masterminds/goutils v1.1.1 github.com/hashicorp/terraform-plugin-framework v1.11.0 github.com/hashicorp/terraform-plugin-framework-validators v0.13.0 github.com/hashicorp/terraform-plugin-go v0.23.0 diff --git a/go.sum b/go.sum index 17d32f1..5d05b7e 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,7 @@ dario.cat/mergo v1.0.0 h1:AGCNq9Evsj31mOgNPcLyXc+4PNABt905YmuqPYYpBWk= dario.cat/mergo v1.0.0/go.mod h1:uNxQE+84aUszobStD9th8a29P2fMDhsBdgRYvZOxGmk= +github.com/Masterminds/goutils v1.1.1 h1:5nUrii3FMTL5diU80unEVvNevw1nH4+ZV4DSLVJLSYI= +github.com/Masterminds/goutils v1.1.1/go.mod h1:8cTjp+g8YejhMuvIA5y2vz3BpJxksy863GQaJW2MFNU= github.com/Microsoft/go-winio v0.6.1 h1:9/kr64B9VUZrLm5YYwbGtUJnMgqWVOdUAXu6Migciow= github.com/Microsoft/go-winio v0.6.1/go.mod h1:LRdKpFKfdobln8UmuiYcKPot9D2v6svN5+sAH+4kjUM= github.com/ProtonMail/go-crypto v1.1.0-alpha.2 h1:bkyFVUP+ROOARdgCiJzNQo2V2kiB97LyUpzH9P6Hrlg= diff --git a/internal/converter/converter_data_size.go b/internal/converter/converter_data_size.go index e5cc913..0708d97 100644 --- a/internal/converter/converter_data_size.go +++ b/internal/converter/converter_data_size.go @@ -65,11 +65,11 @@ func toBytes(coefficient *big.Float) dataSizeConverter { } var ( - BytesToKibibytes = bytesTo(Kibi) - BytesToMebibytes = bytesTo(Mebi) - BytesToGibibytes = bytesTo(Gibi) - BytesToTebibytes = bytesTo(Tebi) - BytesToPebibytes = bytesTo(Pebi) + KibibytesFromBytes = bytesTo(Kibi) + MebibytesFromBytes = bytesTo(Mebi) + GibibytesFromBytes = bytesTo(Gibi) + TebibytesFromBytes = bytesTo(Tebi) + PebibytesFromBytes = bytesTo(Pebi) KibibytesToBytes = toBytes(Kibi) MebibytesToBytes = toBytes(Mebi) @@ -77,11 +77,11 @@ var ( TebibytesToBytes = toBytes(Tebi) PebibytesToBytes = toBytes(Pebi) - BytesToKilobytes = bytesTo(Kilo) - BytesToMegabytes = bytesTo(Mega) - BytesToGigabytes = bytesTo(Giga) - BytesToTerabytes = bytesTo(Tera) - BytesToPetabytes = bytesTo(Peta) + KilobytesFromBytes = bytesTo(Kilo) + MegabytesFromBytes = bytesTo(Mega) + GigabytesFromBytes = bytesTo(Giga) + TerabytesFromBytes = bytesTo(Tera) + PetabytesFromBytes = bytesTo(Peta) KilobytesToBytes = toBytes(Kilo) MegabytesToBytes = toBytes(Mega) diff --git a/internal/generator/datasize/main.go b/internal/generator/datasize/main.go new file mode 100644 index 0000000..23309c1 --- /dev/null +++ b/internal/generator/datasize/main.go @@ -0,0 +1,125 @@ +/* + * Copyright (c) 2024. Dmitry Starov + * SPDX-License-Identifier: MPL-2.0 + */ + +package datasize + +import ( + "fmt" + "path/filepath" + "strings" + + "github.com/Masterminds/goutils" + + "github.com/dstaroff/terraform-provider-units/internal/generator" +) + +var ( + units = []struct { + Full string + Short string + }{{ + Full: "kibibytes", + Short: "kib", + }, { + Full: "mebibytes", + Short: "mib", + }, { + Full: "gibibytes", + Short: "gib", + }, { + Full: "tebibytes", + Short: "tib", + }, { + Full: "pebibytes", + Short: "pib", + }, { + Full: "kilobytes", + Short: "kb", + }, { + Full: "megabytes", + Short: "mb", + }, { + Full: "gigabytes", + Short: "gb", + }, { + Full: "terabytes", + Short: "tb", + }, { + Full: "petabytes", + Short: "pb", + }} +) + +var _ generator.Generator = &Generator{} + +type Generator struct { + generator.Base +} + +func NewGenerator() *Generator { + return &Generator{ + Base: generator.NewBase(), + } +} + +func (g *Generator) GenerateFunctions() (functionConstructorNames []string) { + var directions []generator.ConversionDirection + { + dirFrom := generator.ConversionDirection{ + Title: "From", + Name: "from", + } + dirTo := generator.ConversionDirection{ + Title: "To", + Name: "to", + } + dirFrom.Opposite = &dirTo + dirTo.Opposite = &dirFrom + + directions = append(directions, dirFrom, dirTo) + } + + var functions []generator.Function + for _, unit := range units { + functions = append(functions, generator.Function{ + Conversion: generator.Conversion{ + Unit: generator.ConversionUnit{ + Title: goutils.CapitalizeFully(unit.Full), + Name: unit.Full, + Short: strings.ToLower(unit.Short), + }, + Directions: directions, + }, + CopyrightInfo: g.CopyrightInfo, + }) + } + + for _, function := range functions { + g.Generate( + filepath.Join(generator.PathDirFunctionsGenerated, fmt.Sprintf("data_size_%s.go", function.Conversion.Unit.Name)), + filepath.Join(generator.PathDirTemplates, "data_size_function.go.gotmpl"), + function, + ) + for _, direction := range function.Conversion.Directions { + functionConstructorNames = append( + functionConstructorNames, + fmt.Sprintf("New%s%sModel", direction.Title, function.Conversion.Unit.Title), + ) + } + + for _, direction := range directions { + function.Conversion.Directions = []generator.ConversionDirection{ + direction, + } + g.Generate( + filepath.Join(generator.PathDirFunctionExamples, fmt.Sprintf("%s_%s", direction.Name, function.Conversion.Unit.Short), "function.tf"), + filepath.Join(generator.PathDirTemplates, "data_size_function_example.tf.gotmpl"), + function, + ) + } + } + + return functionConstructorNames +} diff --git a/internal/generator/generator.go b/internal/generator/generator.go new file mode 100644 index 0000000..78034f8 --- /dev/null +++ b/internal/generator/generator.go @@ -0,0 +1,122 @@ +/* + * Copyright (c) 2024. Dmitry Starov + * SPDX-License-Identifier: MPL-2.0 + */ + +package generator + +import ( + "bytes" + "crypto/sha256" + "io" + "log" + "os" + "path/filepath" + "strings" + "text/template" + "time" +) + +type ( + Function struct { + Conversion Conversion + CopyrightInfo copyrightInfo + } + Conversion struct { + Unit ConversionUnit + Directions []ConversionDirection + } + ConversionUnit struct { + Title string + Name string + Short string + } + ConversionDirection struct { + Title string + Name string + Opposite *ConversionDirection + } + + GeneratedFunctions struct { + Names []string + CopyrightInfo copyrightInfo + } +) + +type Generator interface { + GenerateFunctions() (functionConstructorNames []string) +} + +type copyrightInfo struct { + Author string + Year int +} + +type Base struct { + CopyrightInfo copyrightInfo +} + +func NewBase() Base { + return Base{ + CopyrightInfo: copyrightInfo{ + Author: "Dmitry Starov", + Year: time.Now().Year(), + }, + } +} + +func (b Base) Generate(filename string, templatePath string, data any) { + if err := os.MkdirAll(filepath.Dir(filename), 0755); err != nil { + log.Fatal(err) + } + + checksumBefore := b.getFileChecksum(filename) + + file, err := os.Create(filename) + if err != nil { + log.Fatal(err) + } + defer file.Close() + + t, err := template.ParseFiles(templatePath) + if err != nil { + log.Fatal(err) + } + + err = t.Execute(file, data) + if err != nil { + log.Fatal(err) + } + + checksumAfter := b.getFileChecksum(filename) + if !bytes.Equal(checksumBefore, checksumAfter) { + _, p, _ := strings.Cut(filename, "terraform-provider-units") + log.Println("Generated", p) + } +} + +func (_ Base) getFileChecksum(filename string) []byte { + file, err := os.Open(filename) + if err != nil { + return []byte{} + } + defer file.Close() + + hasher := sha256.New() + if _, err = io.Copy(hasher, file); err != nil { + log.Fatal(err) + } + + return hasher.Sum(nil) +} + +func (b Base) GenerateGeneratedFunctions(functionConstructorNames []string) { + b.Generate( + filepath.Join(PathDirFunctions, "generated.go"), + filepath.Join(PathDirTemplates, "generated_functions.go.gotmpl"), + GeneratedFunctions{ + Names: functionConstructorNames, + CopyrightInfo: b.CopyrightInfo, + }, + ) +} diff --git a/internal/generator/main.go b/internal/generator/main.go new file mode 100644 index 0000000..7d0aac9 --- /dev/null +++ b/internal/generator/main.go @@ -0,0 +1,31 @@ +//go:build generate + +/* + * Copyright (c) 2024. Dmitry Starov + * SPDX-License-Identifier: MPL-2.0 + */ + +package main + +import ( + "github.com/dstaroff/terraform-provider-units/internal/generator" + "github.com/dstaroff/terraform-provider-units/internal/generator/datasize" +) + +var ( + generators = []generator.Generator{ + datasize.NewGenerator(), + } +) + +func main() { + var functionConstructorNames []string + + for _, g := range generators { + functionConstructorNames = append(functionConstructorNames, g.GenerateFunctions()...) + } + + generator.NewBase().GenerateGeneratedFunctions(functionConstructorNames) +} + +//go:generate go run ./${GOFILE} diff --git a/internal/generator/path.go b/internal/generator/path.go new file mode 100644 index 0000000..f2eafdd --- /dev/null +++ b/internal/generator/path.go @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2024. Dmitry Starov + * SPDX-License-Identifier: MPL-2.0 + */ + +package generator + +import ( + "path/filepath" +) + +var ( + PathDirRoot string + PathDirTemplates string + + PathDirExamples string + PathDirFunctionExamples string + + PathDirInternal string + PathDirProvider string + PathDirDataSources string + PathDirFunctions string + PathDirFunctionsGenerated string +) + +func init() { + { + var err error + + PathDirRoot, err = filepath.Abs("../../") + if err != nil { + panic(err) + } + + PathDirTemplates, err = filepath.Abs("./templates") + if err != nil { + panic(err) + } + } + + PathDirExamples = filepath.Join(PathDirRoot, "examples") + PathDirFunctionExamples = filepath.Join(PathDirExamples, "functions") + + PathDirInternal = filepath.Join(PathDirRoot, "internal") + PathDirProvider = filepath.Join(PathDirInternal, "provider") + PathDirDataSources = filepath.Join(PathDirProvider, "datasource") + PathDirFunctions = filepath.Join(PathDirProvider, "function") + PathDirFunctionsGenerated = filepath.Join(PathDirFunctions, "generated") +} diff --git a/internal/generator/templates/data_size_function.go.gotmpl b/internal/generator/templates/data_size_function.go.gotmpl new file mode 100644 index 0000000..9ec7944 --- /dev/null +++ b/internal/generator/templates/data_size_function.go.gotmpl @@ -0,0 +1,68 @@ +{{- /*gotype: github.com/dstaroff/terraform-provider-units/internal/generator.Function*/ -}} +// Code generated by go generate; DO NOT EDIT. +/* + * Copyright (c) {{ .CopyrightInfo.Year }}. {{ .CopyrightInfo.Author }} + * SPDX-License-Identifier: MPL-2.0 + */ + +package generated + +import ( + "context" + + "github.com/hashicorp/terraform-plugin-framework/function" + "github.com/hashicorp/terraform-plugin-framework/types" + + "github.com/dstaroff/terraform-provider-units/internal/converter" +) + +var ( +{{- range $direction := .Conversion.Directions }} + _ function.Function = &{{ $direction.Title }}{{ $.Conversion.Unit.Title }}Model{} +{{- end }} +) + +{{- range $direction := .Conversion.Directions }} + +func New{{ $direction.Title }}{{ $.Conversion.Unit.Title }}Model() function.Function { + return &{{ $direction.Title }}{{ $.Conversion.Unit.Title }}Model{} +} + +type {{ $direction.Title }}{{ $.Conversion.Unit.Title }}Model struct{} + +func (f *{{ $direction.Title }}{{ $.Conversion.Unit.Title }}Model) Metadata(_ context.Context, _ function.MetadataRequest, resp *function.MetadataResponse) { + resp.Name = "{{ $direction.Name }}_{{ $.Conversion.Unit.Short }}" +} + +{{- $unitFrom := $.Conversion.Unit.Name -}} +{{- $unitTo := "bytes" -}} +{{- if eq $direction.Name "to" -}} + {{- $unitFrom = "bytes" -}} + {{- $unitTo = $.Conversion.Unit.Name -}} +{{- end }} + +func (f *{{ $direction.Title }}{{ $.Conversion.Unit.Title }}Model) Definition(_ context.Context, _ function.DefinitionRequest, resp *function.DefinitionResponse) { + resp.Definition = function.Definition{ + Summary: "Converts {{ $unitFrom }} to {{ $unitTo }}", + Description: "Given data size in {{ $unitFrom }}, converts it to {{ $unitTo }}.", + MarkdownDescription: "Given data size in **{{ $unitFrom }}**, converts it to **{{ $unitTo }}**.", + + Parameters: []function.Parameter{ + function.NumberParameter{ + Name: "{{ $unitFrom }}", + Description: "Data size in {{ $unitFrom }}", + MarkdownDescription: "Data size in **{{ $unitFrom }}**", + }, + }, + Return: function.NumberReturn{}, + } +} + +func (f *{{ $direction.Title }}{{ $.Conversion.Unit.Title }}Model) Run(ctx context.Context, req function.RunRequest, resp *function.RunResponse) { + var {{ $unitFrom }} types.Number + + resp.Error = function.ConcatFuncErrors(resp.Error, req.Arguments.Get(ctx, &{{ $unitFrom }})) + resp.Error = function.ConcatFuncErrors(resp.Error, resp.Result.Set(ctx, converter.{{ $.Conversion.Unit.Title }}{{ $direction.Opposite.Title }}Bytes({{ $unitFrom }}))) +} + +{{- end }} diff --git a/internal/generator/templates/data_size_function_example.tf.gotmpl b/internal/generator/templates/data_size_function_example.tf.gotmpl new file mode 100644 index 0000000..65e61da --- /dev/null +++ b/internal/generator/templates/data_size_function_example.tf.gotmpl @@ -0,0 +1,15 @@ +{{- /*gotype: github.com/dstaroff/terraform-provider-units/internal/generator.Function*/ -}} +{{- range $direction := .Conversion.Directions }} + +{{- $unitFrom := $.Conversion.Unit.Name -}} +{{- $unitTo := "bytes" -}} +{{- if eq $direction.Name "to" -}} + {{- $unitFrom = "bytes" -}} + {{- $unitTo = $.Conversion.Unit.Name -}} +{{- end -}} + +output "example" { + size_in_{{ $unitTo }} = provider::units::{{ $direction.Name }}_{{ $.Conversion.Unit.Short }}(42) +} + +{{- end -}} diff --git a/internal/generator/templates/generated_functions.go.gotmpl b/internal/generator/templates/generated_functions.go.gotmpl new file mode 100644 index 0000000..acf3808 --- /dev/null +++ b/internal/generator/templates/generated_functions.go.gotmpl @@ -0,0 +1,28 @@ +{{- /*gotype: github.com/dstaroff/terraform-provider-units/internal/generator.GeneratedFunctions*/ -}} +// Code generated by go generate; DO NOT EDIT. +/* + * Copyright (c) {{ .CopyrightInfo.Year }}. {{ .CopyrightInfo.Author }} + * SPDX-License-Identifier: MPL-2.0 + */ + +package function + +import ( + "github.com/hashicorp/terraform-plugin-framework/function" + + genFuncs "github.com/dstaroff/terraform-provider-units/internal/provider/function/generated" +) + +var GeneratedFunctions []func() function.Function + +{{- if gt (len .Names) 0 }} + +func init() { + GeneratedFunctions = append(GeneratedFunctions, +{{- range .Names }} + genFuncs.{{ . }}, +{{- end }} + ) +} + +{{- end }} diff --git a/internal/provider/datasource/data_size.go b/internal/provider/datasource/data_size.go index fb4ca03..86c398b 100644 --- a/internal/provider/datasource/data_size.go +++ b/internal/provider/datasource/data_size.go @@ -98,17 +98,17 @@ func (m *DataSizeModel) Convert() { m.Bytes = bytes - m.Kibibytes = converter.BytesToKibibytes(bytes) - m.Mebibytes = converter.BytesToMebibytes(bytes) - m.Gibibytes = converter.BytesToGibibytes(bytes) - m.Tebibytes = converter.BytesToTebibytes(bytes) - m.Pebibytes = converter.BytesToPebibytes(bytes) - - m.Kilobytes = converter.BytesToKilobytes(bytes) - m.Megabytes = converter.BytesToMegabytes(bytes) - m.Gigabytes = converter.BytesToGigabytes(bytes) - m.Terabytes = converter.BytesToTerabytes(bytes) - m.Petabytes = converter.BytesToPetabytes(bytes) + m.Kibibytes = converter.KibibytesFromBytes(bytes) + m.Mebibytes = converter.MebibytesFromBytes(bytes) + m.Gibibytes = converter.GibibytesFromBytes(bytes) + m.Tebibytes = converter.TebibytesFromBytes(bytes) + m.Pebibytes = converter.PebibytesFromBytes(bytes) + + m.Kilobytes = converter.KilobytesFromBytes(bytes) + m.Megabytes = converter.MegabytesFromBytes(bytes) + m.Gigabytes = converter.GigabytesFromBytes(bytes) + m.Terabytes = converter.TerabytesFromBytes(bytes) + m.Petabytes = converter.PetabytesFromBytes(bytes) } func (d *DataSize) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { diff --git a/internal/provider/datasource/data_size_test.go b/internal/provider/datasource/data_size_test.go index 841d959..6a8093b 100644 --- a/internal/provider/datasource/data_size_test.go +++ b/internal/provider/datasource/data_size_test.go @@ -14,7 +14,7 @@ import ( "github.com/dstaroff/terraform-provider-units/internal/testutils" ) -func TestAccDataSizeBase1000(t *testing.T) { +func TestAccDataSizeDataSource_1000(t *testing.T) { for _, config := range []string{ // language=hcl-terraform ` @@ -52,7 +52,7 @@ func TestAccDataSizeBase1000(t *testing.T) { } } -func TestAccDataSizeBase1024(t *testing.T) { +func TestAccDataSizeDataSource_1024(t *testing.T) { for _, config := range []string{ // language=hcl-terraform ` @@ -90,7 +90,7 @@ func TestAccDataSizeBase1024(t *testing.T) { } } -func TestAccDataSizeFromZero(t *testing.T) { +func TestAccDataSizeDataSource_FromZero(t *testing.T) { for _, config := range []string{ // language=hcl-terraform ` @@ -128,7 +128,7 @@ func TestAccDataSizeFromZero(t *testing.T) { } } -func TestAccDataSizeMultipleAttributesProvided(t *testing.T) { +func TestAccDataSizeDataSource_MultipleAttributesProvided(t *testing.T) { const config = // language=hcl-terraform ` @@ -147,7 +147,7 @@ func TestAccDataSizeMultipleAttributesProvided(t *testing.T) { }) } -func TestAccDataSizeNoAttributesProvided(t *testing.T) { +func TestAccDataSizeDataSource_NoAttributesProvided(t *testing.T) { const config = // language=hcl-terraform ` diff --git a/internal/provider/function/data_size_test.go b/internal/provider/function/data_size_test.go new file mode 100644 index 0000000..4bf6d9c --- /dev/null +++ b/internal/provider/function/data_size_test.go @@ -0,0 +1,185 @@ +/* + * Copyright (c) 2024. Dmitry Starov + * SPDX-License-Identifier: MPL-2.0 + */ + +package function_test + +import ( + "fmt" + "regexp" + "strconv" + "testing" + + "github.com/hashicorp/terraform-plugin-testing/helper/resource" + "github.com/hashicorp/terraform-plugin-testing/tfversion" + + "github.com/dstaroff/terraform-provider-units/internal/testutils" +) + +func TestAccDataSizeFunctions(t *testing.T) { + type testCaseType struct { + config string + result string + } + var testCases []testCaseType + + for _, base := range []int{1000, 1024} { + var abbr string + if base == 1024 { + abbr = "i" + } + + for i, unitPrefix := range []string{"k", "m", "g", "t", "p"} { + result := base + for j := 1; j <= i; j++ { + result *= base + } + + testCases = append(testCases, testCaseType{ + config: fmt.Sprintf( + // language=hcl-terraform + ` + output "test" { + value = provider::units::from_%s%sb(1) + } + `, unitPrefix, abbr, + ), + result: strconv.Itoa(result), + }, testCaseType{ + config: fmt.Sprintf( + // language=hcl-terraform + ` + output "test" { + value = provider::units::to_%s%sb(%d) + } + `, unitPrefix, abbr, result, + ), + result: strconv.Itoa(1), + }) + } + } + + for _, tc := range testCases { + resource.UnitTest(t, resource.TestCase{ + TerraformVersionChecks: []tfversion.TerraformVersionCheck{ + tfversion.SkipBelow(tfversion.Version1_8_0), + }, + ProtoV6ProviderFactories: testutils.TestAccProtoV6ProviderFactories, + Steps: []resource.TestStep{ + { + Config: tc.config, + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckOutput("test", tc.result), + ), + }, + }, + }) + } +} + +func TestAccDataSizeFunctions_0(t *testing.T) { + type testCaseType struct { + config string + } + var testCases []testCaseType + + result := strconv.Itoa(0) + + for _, base := range []int{1000, 1024} { + var abbr string + if base == 1024 { + abbr = "i" + } + + for _, unitPrefix := range []string{"k", "m", "g", "t", "p"} { + testCases = append(testCases, testCaseType{ + config: fmt.Sprintf( + // language=hcl-terraform + ` + output "test" { + value = provider::units::from_%s%sb(0) + } + `, unitPrefix, abbr, + ), + }, testCaseType{ + config: fmt.Sprintf( + // language=hcl-terraform + ` + output "test" { + value = provider::units::to_%s%sb(0) + } + `, unitPrefix, abbr, + ), + }) + } + } + + for _, tc := range testCases { + resource.UnitTest(t, resource.TestCase{ + TerraformVersionChecks: []tfversion.TerraformVersionCheck{ + tfversion.SkipBelow(tfversion.Version1_8_0), + }, + ProtoV6ProviderFactories: testutils.TestAccProtoV6ProviderFactories, + Steps: []resource.TestStep{ + { + Config: tc.config, + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckOutput("test", result), + ), + }, + }, + }) + } +} + +func TestAccDataSizeFunctions_null(t *testing.T) { + type testCaseType struct { + config string + } + var testCases []testCaseType + + for _, base := range []int{1000, 1024} { + var abbr string + if base == 1024 { + abbr = "i" + } + + for _, unitPrefix := range []string{"k", "m", "g", "t", "p"} { + testCases = append(testCases, testCaseType{ + config: fmt.Sprintf( + // language=hcl-terraform + ` + output "test" { + value = provider::units::from_%s%sb(null) + } + `, unitPrefix, abbr, + ), + }, testCaseType{ + config: fmt.Sprintf( + // language=hcl-terraform + ` + output "test" { + value = provider::units::to_%s%sb(null) + } + `, unitPrefix, abbr, + ), + }) + } + } + + for _, tc := range testCases { + resource.UnitTest(t, resource.TestCase{ + TerraformVersionChecks: []tfversion.TerraformVersionCheck{ + tfversion.SkipBelow(tfversion.Version1_8_0), + }, + ProtoV6ProviderFactories: testutils.TestAccProtoV6ProviderFactories, + Steps: []resource.TestStep{ + { + Config: tc.config, + ExpectError: regexp.MustCompile(`argument must not be null`), + }, + }, + }) + } +} diff --git a/internal/provider/function/generated.go b/internal/provider/function/generated.go new file mode 100644 index 0000000..d44e0db --- /dev/null +++ b/internal/provider/function/generated.go @@ -0,0 +1,40 @@ +// Code generated by go generate; DO NOT EDIT. +/* + * Copyright (c) 2024. Dmitry Starov + * SPDX-License-Identifier: MPL-2.0 + */ + +package function + +import ( + "github.com/hashicorp/terraform-plugin-framework/function" + + genFuncs "github.com/dstaroff/terraform-provider-units/internal/provider/function/generated" +) + +var GeneratedFunctions []func() function.Function + +func init() { + GeneratedFunctions = append(GeneratedFunctions, + genFuncs.NewFromKibibytesModel, + genFuncs.NewToKibibytesModel, + genFuncs.NewFromMebibytesModel, + genFuncs.NewToMebibytesModel, + genFuncs.NewFromGibibytesModel, + genFuncs.NewToGibibytesModel, + genFuncs.NewFromTebibytesModel, + genFuncs.NewToTebibytesModel, + genFuncs.NewFromPebibytesModel, + genFuncs.NewToPebibytesModel, + genFuncs.NewFromKilobytesModel, + genFuncs.NewToKilobytesModel, + genFuncs.NewFromMegabytesModel, + genFuncs.NewToMegabytesModel, + genFuncs.NewFromGigabytesModel, + genFuncs.NewToGigabytesModel, + genFuncs.NewFromTerabytesModel, + genFuncs.NewToTerabytesModel, + genFuncs.NewFromPetabytesModel, + genFuncs.NewToPetabytesModel, + ) +} diff --git a/internal/provider/function/generated/data_size_gibibytes.go b/internal/provider/function/generated/data_size_gibibytes.go new file mode 100644 index 0000000..86080bd --- /dev/null +++ b/internal/provider/function/generated/data_size_gibibytes.go @@ -0,0 +1,89 @@ +// Code generated by go generate; DO NOT EDIT. +/* + * Copyright (c) 2024. Dmitry Starov + * SPDX-License-Identifier: MPL-2.0 + */ + +package generated + +import ( + "context" + + "github.com/hashicorp/terraform-plugin-framework/function" + "github.com/hashicorp/terraform-plugin-framework/types" + + "github.com/dstaroff/terraform-provider-units/internal/converter" +) + +var ( + _ function.Function = &FromGibibytesModel{} + _ function.Function = &ToGibibytesModel{} +) + +func NewFromGibibytesModel() function.Function { + return &FromGibibytesModel{} +} + +type FromGibibytesModel struct{} + +func (f *FromGibibytesModel) Metadata(_ context.Context, _ function.MetadataRequest, resp *function.MetadataResponse) { + resp.Name = "from_gib" +} + +func (f *FromGibibytesModel) Definition(_ context.Context, _ function.DefinitionRequest, resp *function.DefinitionResponse) { + resp.Definition = function.Definition{ + Summary: "Converts gibibytes to bytes", + Description: "Given data size in gibibytes, converts it to bytes.", + MarkdownDescription: "Given data size in **gibibytes**, converts it to **bytes**.", + + Parameters: []function.Parameter{ + function.NumberParameter{ + Name: "gibibytes", + Description: "Data size in gibibytes", + MarkdownDescription: "Data size in **gibibytes**", + }, + }, + Return: function.NumberReturn{}, + } +} + +func (f *FromGibibytesModel) Run(ctx context.Context, req function.RunRequest, resp *function.RunResponse) { + var gibibytes types.Number + + resp.Error = function.ConcatFuncErrors(resp.Error, req.Arguments.Get(ctx, &gibibytes)) + resp.Error = function.ConcatFuncErrors(resp.Error, resp.Result.Set(ctx, converter.GibibytesToBytes(gibibytes))) +} + +func NewToGibibytesModel() function.Function { + return &ToGibibytesModel{} +} + +type ToGibibytesModel struct{} + +func (f *ToGibibytesModel) Metadata(_ context.Context, _ function.MetadataRequest, resp *function.MetadataResponse) { + resp.Name = "to_gib" +} + +func (f *ToGibibytesModel) Definition(_ context.Context, _ function.DefinitionRequest, resp *function.DefinitionResponse) { + resp.Definition = function.Definition{ + Summary: "Converts bytes to gibibytes", + Description: "Given data size in bytes, converts it to gibibytes.", + MarkdownDescription: "Given data size in **bytes**, converts it to **gibibytes**.", + + Parameters: []function.Parameter{ + function.NumberParameter{ + Name: "bytes", + Description: "Data size in bytes", + MarkdownDescription: "Data size in **bytes**", + }, + }, + Return: function.NumberReturn{}, + } +} + +func (f *ToGibibytesModel) Run(ctx context.Context, req function.RunRequest, resp *function.RunResponse) { + var bytes types.Number + + resp.Error = function.ConcatFuncErrors(resp.Error, req.Arguments.Get(ctx, &bytes)) + resp.Error = function.ConcatFuncErrors(resp.Error, resp.Result.Set(ctx, converter.GibibytesFromBytes(bytes))) +} diff --git a/internal/provider/function/generated/data_size_gigabytes.go b/internal/provider/function/generated/data_size_gigabytes.go new file mode 100644 index 0000000..8252e0b --- /dev/null +++ b/internal/provider/function/generated/data_size_gigabytes.go @@ -0,0 +1,89 @@ +// Code generated by go generate; DO NOT EDIT. +/* + * Copyright (c) 2024. Dmitry Starov + * SPDX-License-Identifier: MPL-2.0 + */ + +package generated + +import ( + "context" + + "github.com/hashicorp/terraform-plugin-framework/function" + "github.com/hashicorp/terraform-plugin-framework/types" + + "github.com/dstaroff/terraform-provider-units/internal/converter" +) + +var ( + _ function.Function = &FromGigabytesModel{} + _ function.Function = &ToGigabytesModel{} +) + +func NewFromGigabytesModel() function.Function { + return &FromGigabytesModel{} +} + +type FromGigabytesModel struct{} + +func (f *FromGigabytesModel) Metadata(_ context.Context, _ function.MetadataRequest, resp *function.MetadataResponse) { + resp.Name = "from_gb" +} + +func (f *FromGigabytesModel) Definition(_ context.Context, _ function.DefinitionRequest, resp *function.DefinitionResponse) { + resp.Definition = function.Definition{ + Summary: "Converts gigabytes to bytes", + Description: "Given data size in gigabytes, converts it to bytes.", + MarkdownDescription: "Given data size in **gigabytes**, converts it to **bytes**.", + + Parameters: []function.Parameter{ + function.NumberParameter{ + Name: "gigabytes", + Description: "Data size in gigabytes", + MarkdownDescription: "Data size in **gigabytes**", + }, + }, + Return: function.NumberReturn{}, + } +} + +func (f *FromGigabytesModel) Run(ctx context.Context, req function.RunRequest, resp *function.RunResponse) { + var gigabytes types.Number + + resp.Error = function.ConcatFuncErrors(resp.Error, req.Arguments.Get(ctx, &gigabytes)) + resp.Error = function.ConcatFuncErrors(resp.Error, resp.Result.Set(ctx, converter.GigabytesToBytes(gigabytes))) +} + +func NewToGigabytesModel() function.Function { + return &ToGigabytesModel{} +} + +type ToGigabytesModel struct{} + +func (f *ToGigabytesModel) Metadata(_ context.Context, _ function.MetadataRequest, resp *function.MetadataResponse) { + resp.Name = "to_gb" +} + +func (f *ToGigabytesModel) Definition(_ context.Context, _ function.DefinitionRequest, resp *function.DefinitionResponse) { + resp.Definition = function.Definition{ + Summary: "Converts bytes to gigabytes", + Description: "Given data size in bytes, converts it to gigabytes.", + MarkdownDescription: "Given data size in **bytes**, converts it to **gigabytes**.", + + Parameters: []function.Parameter{ + function.NumberParameter{ + Name: "bytes", + Description: "Data size in bytes", + MarkdownDescription: "Data size in **bytes**", + }, + }, + Return: function.NumberReturn{}, + } +} + +func (f *ToGigabytesModel) Run(ctx context.Context, req function.RunRequest, resp *function.RunResponse) { + var bytes types.Number + + resp.Error = function.ConcatFuncErrors(resp.Error, req.Arguments.Get(ctx, &bytes)) + resp.Error = function.ConcatFuncErrors(resp.Error, resp.Result.Set(ctx, converter.GigabytesFromBytes(bytes))) +} diff --git a/internal/provider/function/generated/data_size_kibibytes.go b/internal/provider/function/generated/data_size_kibibytes.go new file mode 100644 index 0000000..669102c --- /dev/null +++ b/internal/provider/function/generated/data_size_kibibytes.go @@ -0,0 +1,89 @@ +// Code generated by go generate; DO NOT EDIT. +/* + * Copyright (c) 2024. Dmitry Starov + * SPDX-License-Identifier: MPL-2.0 + */ + +package generated + +import ( + "context" + + "github.com/hashicorp/terraform-plugin-framework/function" + "github.com/hashicorp/terraform-plugin-framework/types" + + "github.com/dstaroff/terraform-provider-units/internal/converter" +) + +var ( + _ function.Function = &FromKibibytesModel{} + _ function.Function = &ToKibibytesModel{} +) + +func NewFromKibibytesModel() function.Function { + return &FromKibibytesModel{} +} + +type FromKibibytesModel struct{} + +func (f *FromKibibytesModel) Metadata(_ context.Context, _ function.MetadataRequest, resp *function.MetadataResponse) { + resp.Name = "from_kib" +} + +func (f *FromKibibytesModel) Definition(_ context.Context, _ function.DefinitionRequest, resp *function.DefinitionResponse) { + resp.Definition = function.Definition{ + Summary: "Converts kibibytes to bytes", + Description: "Given data size in kibibytes, converts it to bytes.", + MarkdownDescription: "Given data size in **kibibytes**, converts it to **bytes**.", + + Parameters: []function.Parameter{ + function.NumberParameter{ + Name: "kibibytes", + Description: "Data size in kibibytes", + MarkdownDescription: "Data size in **kibibytes**", + }, + }, + Return: function.NumberReturn{}, + } +} + +func (f *FromKibibytesModel) Run(ctx context.Context, req function.RunRequest, resp *function.RunResponse) { + var kibibytes types.Number + + resp.Error = function.ConcatFuncErrors(resp.Error, req.Arguments.Get(ctx, &kibibytes)) + resp.Error = function.ConcatFuncErrors(resp.Error, resp.Result.Set(ctx, converter.KibibytesToBytes(kibibytes))) +} + +func NewToKibibytesModel() function.Function { + return &ToKibibytesModel{} +} + +type ToKibibytesModel struct{} + +func (f *ToKibibytesModel) Metadata(_ context.Context, _ function.MetadataRequest, resp *function.MetadataResponse) { + resp.Name = "to_kib" +} + +func (f *ToKibibytesModel) Definition(_ context.Context, _ function.DefinitionRequest, resp *function.DefinitionResponse) { + resp.Definition = function.Definition{ + Summary: "Converts bytes to kibibytes", + Description: "Given data size in bytes, converts it to kibibytes.", + MarkdownDescription: "Given data size in **bytes**, converts it to **kibibytes**.", + + Parameters: []function.Parameter{ + function.NumberParameter{ + Name: "bytes", + Description: "Data size in bytes", + MarkdownDescription: "Data size in **bytes**", + }, + }, + Return: function.NumberReturn{}, + } +} + +func (f *ToKibibytesModel) Run(ctx context.Context, req function.RunRequest, resp *function.RunResponse) { + var bytes types.Number + + resp.Error = function.ConcatFuncErrors(resp.Error, req.Arguments.Get(ctx, &bytes)) + resp.Error = function.ConcatFuncErrors(resp.Error, resp.Result.Set(ctx, converter.KibibytesFromBytes(bytes))) +} diff --git a/internal/provider/function/generated/data_size_kilobytes.go b/internal/provider/function/generated/data_size_kilobytes.go new file mode 100644 index 0000000..4000df8 --- /dev/null +++ b/internal/provider/function/generated/data_size_kilobytes.go @@ -0,0 +1,89 @@ +// Code generated by go generate; DO NOT EDIT. +/* + * Copyright (c) 2024. Dmitry Starov + * SPDX-License-Identifier: MPL-2.0 + */ + +package generated + +import ( + "context" + + "github.com/hashicorp/terraform-plugin-framework/function" + "github.com/hashicorp/terraform-plugin-framework/types" + + "github.com/dstaroff/terraform-provider-units/internal/converter" +) + +var ( + _ function.Function = &FromKilobytesModel{} + _ function.Function = &ToKilobytesModel{} +) + +func NewFromKilobytesModel() function.Function { + return &FromKilobytesModel{} +} + +type FromKilobytesModel struct{} + +func (f *FromKilobytesModel) Metadata(_ context.Context, _ function.MetadataRequest, resp *function.MetadataResponse) { + resp.Name = "from_kb" +} + +func (f *FromKilobytesModel) Definition(_ context.Context, _ function.DefinitionRequest, resp *function.DefinitionResponse) { + resp.Definition = function.Definition{ + Summary: "Converts kilobytes to bytes", + Description: "Given data size in kilobytes, converts it to bytes.", + MarkdownDescription: "Given data size in **kilobytes**, converts it to **bytes**.", + + Parameters: []function.Parameter{ + function.NumberParameter{ + Name: "kilobytes", + Description: "Data size in kilobytes", + MarkdownDescription: "Data size in **kilobytes**", + }, + }, + Return: function.NumberReturn{}, + } +} + +func (f *FromKilobytesModel) Run(ctx context.Context, req function.RunRequest, resp *function.RunResponse) { + var kilobytes types.Number + + resp.Error = function.ConcatFuncErrors(resp.Error, req.Arguments.Get(ctx, &kilobytes)) + resp.Error = function.ConcatFuncErrors(resp.Error, resp.Result.Set(ctx, converter.KilobytesToBytes(kilobytes))) +} + +func NewToKilobytesModel() function.Function { + return &ToKilobytesModel{} +} + +type ToKilobytesModel struct{} + +func (f *ToKilobytesModel) Metadata(_ context.Context, _ function.MetadataRequest, resp *function.MetadataResponse) { + resp.Name = "to_kb" +} + +func (f *ToKilobytesModel) Definition(_ context.Context, _ function.DefinitionRequest, resp *function.DefinitionResponse) { + resp.Definition = function.Definition{ + Summary: "Converts bytes to kilobytes", + Description: "Given data size in bytes, converts it to kilobytes.", + MarkdownDescription: "Given data size in **bytes**, converts it to **kilobytes**.", + + Parameters: []function.Parameter{ + function.NumberParameter{ + Name: "bytes", + Description: "Data size in bytes", + MarkdownDescription: "Data size in **bytes**", + }, + }, + Return: function.NumberReturn{}, + } +} + +func (f *ToKilobytesModel) Run(ctx context.Context, req function.RunRequest, resp *function.RunResponse) { + var bytes types.Number + + resp.Error = function.ConcatFuncErrors(resp.Error, req.Arguments.Get(ctx, &bytes)) + resp.Error = function.ConcatFuncErrors(resp.Error, resp.Result.Set(ctx, converter.KilobytesFromBytes(bytes))) +} diff --git a/internal/provider/function/generated/data_size_mebibytes.go b/internal/provider/function/generated/data_size_mebibytes.go new file mode 100644 index 0000000..a2a70a0 --- /dev/null +++ b/internal/provider/function/generated/data_size_mebibytes.go @@ -0,0 +1,89 @@ +// Code generated by go generate; DO NOT EDIT. +/* + * Copyright (c) 2024. Dmitry Starov + * SPDX-License-Identifier: MPL-2.0 + */ + +package generated + +import ( + "context" + + "github.com/hashicorp/terraform-plugin-framework/function" + "github.com/hashicorp/terraform-plugin-framework/types" + + "github.com/dstaroff/terraform-provider-units/internal/converter" +) + +var ( + _ function.Function = &FromMebibytesModel{} + _ function.Function = &ToMebibytesModel{} +) + +func NewFromMebibytesModel() function.Function { + return &FromMebibytesModel{} +} + +type FromMebibytesModel struct{} + +func (f *FromMebibytesModel) Metadata(_ context.Context, _ function.MetadataRequest, resp *function.MetadataResponse) { + resp.Name = "from_mib" +} + +func (f *FromMebibytesModel) Definition(_ context.Context, _ function.DefinitionRequest, resp *function.DefinitionResponse) { + resp.Definition = function.Definition{ + Summary: "Converts mebibytes to bytes", + Description: "Given data size in mebibytes, converts it to bytes.", + MarkdownDescription: "Given data size in **mebibytes**, converts it to **bytes**.", + + Parameters: []function.Parameter{ + function.NumberParameter{ + Name: "mebibytes", + Description: "Data size in mebibytes", + MarkdownDescription: "Data size in **mebibytes**", + }, + }, + Return: function.NumberReturn{}, + } +} + +func (f *FromMebibytesModel) Run(ctx context.Context, req function.RunRequest, resp *function.RunResponse) { + var mebibytes types.Number + + resp.Error = function.ConcatFuncErrors(resp.Error, req.Arguments.Get(ctx, &mebibytes)) + resp.Error = function.ConcatFuncErrors(resp.Error, resp.Result.Set(ctx, converter.MebibytesToBytes(mebibytes))) +} + +func NewToMebibytesModel() function.Function { + return &ToMebibytesModel{} +} + +type ToMebibytesModel struct{} + +func (f *ToMebibytesModel) Metadata(_ context.Context, _ function.MetadataRequest, resp *function.MetadataResponse) { + resp.Name = "to_mib" +} + +func (f *ToMebibytesModel) Definition(_ context.Context, _ function.DefinitionRequest, resp *function.DefinitionResponse) { + resp.Definition = function.Definition{ + Summary: "Converts bytes to mebibytes", + Description: "Given data size in bytes, converts it to mebibytes.", + MarkdownDescription: "Given data size in **bytes**, converts it to **mebibytes**.", + + Parameters: []function.Parameter{ + function.NumberParameter{ + Name: "bytes", + Description: "Data size in bytes", + MarkdownDescription: "Data size in **bytes**", + }, + }, + Return: function.NumberReturn{}, + } +} + +func (f *ToMebibytesModel) Run(ctx context.Context, req function.RunRequest, resp *function.RunResponse) { + var bytes types.Number + + resp.Error = function.ConcatFuncErrors(resp.Error, req.Arguments.Get(ctx, &bytes)) + resp.Error = function.ConcatFuncErrors(resp.Error, resp.Result.Set(ctx, converter.MebibytesFromBytes(bytes))) +} diff --git a/internal/provider/function/generated/data_size_megabytes.go b/internal/provider/function/generated/data_size_megabytes.go new file mode 100644 index 0000000..c5f4b7e --- /dev/null +++ b/internal/provider/function/generated/data_size_megabytes.go @@ -0,0 +1,89 @@ +// Code generated by go generate; DO NOT EDIT. +/* + * Copyright (c) 2024. Dmitry Starov + * SPDX-License-Identifier: MPL-2.0 + */ + +package generated + +import ( + "context" + + "github.com/hashicorp/terraform-plugin-framework/function" + "github.com/hashicorp/terraform-plugin-framework/types" + + "github.com/dstaroff/terraform-provider-units/internal/converter" +) + +var ( + _ function.Function = &FromMegabytesModel{} + _ function.Function = &ToMegabytesModel{} +) + +func NewFromMegabytesModel() function.Function { + return &FromMegabytesModel{} +} + +type FromMegabytesModel struct{} + +func (f *FromMegabytesModel) Metadata(_ context.Context, _ function.MetadataRequest, resp *function.MetadataResponse) { + resp.Name = "from_mb" +} + +func (f *FromMegabytesModel) Definition(_ context.Context, _ function.DefinitionRequest, resp *function.DefinitionResponse) { + resp.Definition = function.Definition{ + Summary: "Converts megabytes to bytes", + Description: "Given data size in megabytes, converts it to bytes.", + MarkdownDescription: "Given data size in **megabytes**, converts it to **bytes**.", + + Parameters: []function.Parameter{ + function.NumberParameter{ + Name: "megabytes", + Description: "Data size in megabytes", + MarkdownDescription: "Data size in **megabytes**", + }, + }, + Return: function.NumberReturn{}, + } +} + +func (f *FromMegabytesModel) Run(ctx context.Context, req function.RunRequest, resp *function.RunResponse) { + var megabytes types.Number + + resp.Error = function.ConcatFuncErrors(resp.Error, req.Arguments.Get(ctx, &megabytes)) + resp.Error = function.ConcatFuncErrors(resp.Error, resp.Result.Set(ctx, converter.MegabytesToBytes(megabytes))) +} + +func NewToMegabytesModel() function.Function { + return &ToMegabytesModel{} +} + +type ToMegabytesModel struct{} + +func (f *ToMegabytesModel) Metadata(_ context.Context, _ function.MetadataRequest, resp *function.MetadataResponse) { + resp.Name = "to_mb" +} + +func (f *ToMegabytesModel) Definition(_ context.Context, _ function.DefinitionRequest, resp *function.DefinitionResponse) { + resp.Definition = function.Definition{ + Summary: "Converts bytes to megabytes", + Description: "Given data size in bytes, converts it to megabytes.", + MarkdownDescription: "Given data size in **bytes**, converts it to **megabytes**.", + + Parameters: []function.Parameter{ + function.NumberParameter{ + Name: "bytes", + Description: "Data size in bytes", + MarkdownDescription: "Data size in **bytes**", + }, + }, + Return: function.NumberReturn{}, + } +} + +func (f *ToMegabytesModel) Run(ctx context.Context, req function.RunRequest, resp *function.RunResponse) { + var bytes types.Number + + resp.Error = function.ConcatFuncErrors(resp.Error, req.Arguments.Get(ctx, &bytes)) + resp.Error = function.ConcatFuncErrors(resp.Error, resp.Result.Set(ctx, converter.MegabytesFromBytes(bytes))) +} diff --git a/internal/provider/function/generated/data_size_pebibytes.go b/internal/provider/function/generated/data_size_pebibytes.go new file mode 100644 index 0000000..17cb8c3 --- /dev/null +++ b/internal/provider/function/generated/data_size_pebibytes.go @@ -0,0 +1,89 @@ +// Code generated by go generate; DO NOT EDIT. +/* + * Copyright (c) 2024. Dmitry Starov + * SPDX-License-Identifier: MPL-2.0 + */ + +package generated + +import ( + "context" + + "github.com/hashicorp/terraform-plugin-framework/function" + "github.com/hashicorp/terraform-plugin-framework/types" + + "github.com/dstaroff/terraform-provider-units/internal/converter" +) + +var ( + _ function.Function = &FromPebibytesModel{} + _ function.Function = &ToPebibytesModel{} +) + +func NewFromPebibytesModel() function.Function { + return &FromPebibytesModel{} +} + +type FromPebibytesModel struct{} + +func (f *FromPebibytesModel) Metadata(_ context.Context, _ function.MetadataRequest, resp *function.MetadataResponse) { + resp.Name = "from_pib" +} + +func (f *FromPebibytesModel) Definition(_ context.Context, _ function.DefinitionRequest, resp *function.DefinitionResponse) { + resp.Definition = function.Definition{ + Summary: "Converts pebibytes to bytes", + Description: "Given data size in pebibytes, converts it to bytes.", + MarkdownDescription: "Given data size in **pebibytes**, converts it to **bytes**.", + + Parameters: []function.Parameter{ + function.NumberParameter{ + Name: "pebibytes", + Description: "Data size in pebibytes", + MarkdownDescription: "Data size in **pebibytes**", + }, + }, + Return: function.NumberReturn{}, + } +} + +func (f *FromPebibytesModel) Run(ctx context.Context, req function.RunRequest, resp *function.RunResponse) { + var pebibytes types.Number + + resp.Error = function.ConcatFuncErrors(resp.Error, req.Arguments.Get(ctx, &pebibytes)) + resp.Error = function.ConcatFuncErrors(resp.Error, resp.Result.Set(ctx, converter.PebibytesToBytes(pebibytes))) +} + +func NewToPebibytesModel() function.Function { + return &ToPebibytesModel{} +} + +type ToPebibytesModel struct{} + +func (f *ToPebibytesModel) Metadata(_ context.Context, _ function.MetadataRequest, resp *function.MetadataResponse) { + resp.Name = "to_pib" +} + +func (f *ToPebibytesModel) Definition(_ context.Context, _ function.DefinitionRequest, resp *function.DefinitionResponse) { + resp.Definition = function.Definition{ + Summary: "Converts bytes to pebibytes", + Description: "Given data size in bytes, converts it to pebibytes.", + MarkdownDescription: "Given data size in **bytes**, converts it to **pebibytes**.", + + Parameters: []function.Parameter{ + function.NumberParameter{ + Name: "bytes", + Description: "Data size in bytes", + MarkdownDescription: "Data size in **bytes**", + }, + }, + Return: function.NumberReturn{}, + } +} + +func (f *ToPebibytesModel) Run(ctx context.Context, req function.RunRequest, resp *function.RunResponse) { + var bytes types.Number + + resp.Error = function.ConcatFuncErrors(resp.Error, req.Arguments.Get(ctx, &bytes)) + resp.Error = function.ConcatFuncErrors(resp.Error, resp.Result.Set(ctx, converter.PebibytesFromBytes(bytes))) +} diff --git a/internal/provider/function/generated/data_size_petabytes.go b/internal/provider/function/generated/data_size_petabytes.go new file mode 100644 index 0000000..5e5c746 --- /dev/null +++ b/internal/provider/function/generated/data_size_petabytes.go @@ -0,0 +1,89 @@ +// Code generated by go generate; DO NOT EDIT. +/* + * Copyright (c) 2024. Dmitry Starov + * SPDX-License-Identifier: MPL-2.0 + */ + +package generated + +import ( + "context" + + "github.com/hashicorp/terraform-plugin-framework/function" + "github.com/hashicorp/terraform-plugin-framework/types" + + "github.com/dstaroff/terraform-provider-units/internal/converter" +) + +var ( + _ function.Function = &FromPetabytesModel{} + _ function.Function = &ToPetabytesModel{} +) + +func NewFromPetabytesModel() function.Function { + return &FromPetabytesModel{} +} + +type FromPetabytesModel struct{} + +func (f *FromPetabytesModel) Metadata(_ context.Context, _ function.MetadataRequest, resp *function.MetadataResponse) { + resp.Name = "from_pb" +} + +func (f *FromPetabytesModel) Definition(_ context.Context, _ function.DefinitionRequest, resp *function.DefinitionResponse) { + resp.Definition = function.Definition{ + Summary: "Converts petabytes to bytes", + Description: "Given data size in petabytes, converts it to bytes.", + MarkdownDescription: "Given data size in **petabytes**, converts it to **bytes**.", + + Parameters: []function.Parameter{ + function.NumberParameter{ + Name: "petabytes", + Description: "Data size in petabytes", + MarkdownDescription: "Data size in **petabytes**", + }, + }, + Return: function.NumberReturn{}, + } +} + +func (f *FromPetabytesModel) Run(ctx context.Context, req function.RunRequest, resp *function.RunResponse) { + var petabytes types.Number + + resp.Error = function.ConcatFuncErrors(resp.Error, req.Arguments.Get(ctx, &petabytes)) + resp.Error = function.ConcatFuncErrors(resp.Error, resp.Result.Set(ctx, converter.PetabytesToBytes(petabytes))) +} + +func NewToPetabytesModel() function.Function { + return &ToPetabytesModel{} +} + +type ToPetabytesModel struct{} + +func (f *ToPetabytesModel) Metadata(_ context.Context, _ function.MetadataRequest, resp *function.MetadataResponse) { + resp.Name = "to_pb" +} + +func (f *ToPetabytesModel) Definition(_ context.Context, _ function.DefinitionRequest, resp *function.DefinitionResponse) { + resp.Definition = function.Definition{ + Summary: "Converts bytes to petabytes", + Description: "Given data size in bytes, converts it to petabytes.", + MarkdownDescription: "Given data size in **bytes**, converts it to **petabytes**.", + + Parameters: []function.Parameter{ + function.NumberParameter{ + Name: "bytes", + Description: "Data size in bytes", + MarkdownDescription: "Data size in **bytes**", + }, + }, + Return: function.NumberReturn{}, + } +} + +func (f *ToPetabytesModel) Run(ctx context.Context, req function.RunRequest, resp *function.RunResponse) { + var bytes types.Number + + resp.Error = function.ConcatFuncErrors(resp.Error, req.Arguments.Get(ctx, &bytes)) + resp.Error = function.ConcatFuncErrors(resp.Error, resp.Result.Set(ctx, converter.PetabytesFromBytes(bytes))) +} diff --git a/internal/provider/function/generated/data_size_tebibytes.go b/internal/provider/function/generated/data_size_tebibytes.go new file mode 100644 index 0000000..e03a177 --- /dev/null +++ b/internal/provider/function/generated/data_size_tebibytes.go @@ -0,0 +1,89 @@ +// Code generated by go generate; DO NOT EDIT. +/* + * Copyright (c) 2024. Dmitry Starov + * SPDX-License-Identifier: MPL-2.0 + */ + +package generated + +import ( + "context" + + "github.com/hashicorp/terraform-plugin-framework/function" + "github.com/hashicorp/terraform-plugin-framework/types" + + "github.com/dstaroff/terraform-provider-units/internal/converter" +) + +var ( + _ function.Function = &FromTebibytesModel{} + _ function.Function = &ToTebibytesModel{} +) + +func NewFromTebibytesModel() function.Function { + return &FromTebibytesModel{} +} + +type FromTebibytesModel struct{} + +func (f *FromTebibytesModel) Metadata(_ context.Context, _ function.MetadataRequest, resp *function.MetadataResponse) { + resp.Name = "from_tib" +} + +func (f *FromTebibytesModel) Definition(_ context.Context, _ function.DefinitionRequest, resp *function.DefinitionResponse) { + resp.Definition = function.Definition{ + Summary: "Converts tebibytes to bytes", + Description: "Given data size in tebibytes, converts it to bytes.", + MarkdownDescription: "Given data size in **tebibytes**, converts it to **bytes**.", + + Parameters: []function.Parameter{ + function.NumberParameter{ + Name: "tebibytes", + Description: "Data size in tebibytes", + MarkdownDescription: "Data size in **tebibytes**", + }, + }, + Return: function.NumberReturn{}, + } +} + +func (f *FromTebibytesModel) Run(ctx context.Context, req function.RunRequest, resp *function.RunResponse) { + var tebibytes types.Number + + resp.Error = function.ConcatFuncErrors(resp.Error, req.Arguments.Get(ctx, &tebibytes)) + resp.Error = function.ConcatFuncErrors(resp.Error, resp.Result.Set(ctx, converter.TebibytesToBytes(tebibytes))) +} + +func NewToTebibytesModel() function.Function { + return &ToTebibytesModel{} +} + +type ToTebibytesModel struct{} + +func (f *ToTebibytesModel) Metadata(_ context.Context, _ function.MetadataRequest, resp *function.MetadataResponse) { + resp.Name = "to_tib" +} + +func (f *ToTebibytesModel) Definition(_ context.Context, _ function.DefinitionRequest, resp *function.DefinitionResponse) { + resp.Definition = function.Definition{ + Summary: "Converts bytes to tebibytes", + Description: "Given data size in bytes, converts it to tebibytes.", + MarkdownDescription: "Given data size in **bytes**, converts it to **tebibytes**.", + + Parameters: []function.Parameter{ + function.NumberParameter{ + Name: "bytes", + Description: "Data size in bytes", + MarkdownDescription: "Data size in **bytes**", + }, + }, + Return: function.NumberReturn{}, + } +} + +func (f *ToTebibytesModel) Run(ctx context.Context, req function.RunRequest, resp *function.RunResponse) { + var bytes types.Number + + resp.Error = function.ConcatFuncErrors(resp.Error, req.Arguments.Get(ctx, &bytes)) + resp.Error = function.ConcatFuncErrors(resp.Error, resp.Result.Set(ctx, converter.TebibytesFromBytes(bytes))) +} diff --git a/internal/provider/function/generated/data_size_terabytes.go b/internal/provider/function/generated/data_size_terabytes.go new file mode 100644 index 0000000..a754404 --- /dev/null +++ b/internal/provider/function/generated/data_size_terabytes.go @@ -0,0 +1,89 @@ +// Code generated by go generate; DO NOT EDIT. +/* + * Copyright (c) 2024. Dmitry Starov + * SPDX-License-Identifier: MPL-2.0 + */ + +package generated + +import ( + "context" + + "github.com/hashicorp/terraform-plugin-framework/function" + "github.com/hashicorp/terraform-plugin-framework/types" + + "github.com/dstaroff/terraform-provider-units/internal/converter" +) + +var ( + _ function.Function = &FromTerabytesModel{} + _ function.Function = &ToTerabytesModel{} +) + +func NewFromTerabytesModel() function.Function { + return &FromTerabytesModel{} +} + +type FromTerabytesModel struct{} + +func (f *FromTerabytesModel) Metadata(_ context.Context, _ function.MetadataRequest, resp *function.MetadataResponse) { + resp.Name = "from_tb" +} + +func (f *FromTerabytesModel) Definition(_ context.Context, _ function.DefinitionRequest, resp *function.DefinitionResponse) { + resp.Definition = function.Definition{ + Summary: "Converts terabytes to bytes", + Description: "Given data size in terabytes, converts it to bytes.", + MarkdownDescription: "Given data size in **terabytes**, converts it to **bytes**.", + + Parameters: []function.Parameter{ + function.NumberParameter{ + Name: "terabytes", + Description: "Data size in terabytes", + MarkdownDescription: "Data size in **terabytes**", + }, + }, + Return: function.NumberReturn{}, + } +} + +func (f *FromTerabytesModel) Run(ctx context.Context, req function.RunRequest, resp *function.RunResponse) { + var terabytes types.Number + + resp.Error = function.ConcatFuncErrors(resp.Error, req.Arguments.Get(ctx, &terabytes)) + resp.Error = function.ConcatFuncErrors(resp.Error, resp.Result.Set(ctx, converter.TerabytesToBytes(terabytes))) +} + +func NewToTerabytesModel() function.Function { + return &ToTerabytesModel{} +} + +type ToTerabytesModel struct{} + +func (f *ToTerabytesModel) Metadata(_ context.Context, _ function.MetadataRequest, resp *function.MetadataResponse) { + resp.Name = "to_tb" +} + +func (f *ToTerabytesModel) Definition(_ context.Context, _ function.DefinitionRequest, resp *function.DefinitionResponse) { + resp.Definition = function.Definition{ + Summary: "Converts bytes to terabytes", + Description: "Given data size in bytes, converts it to terabytes.", + MarkdownDescription: "Given data size in **bytes**, converts it to **terabytes**.", + + Parameters: []function.Parameter{ + function.NumberParameter{ + Name: "bytes", + Description: "Data size in bytes", + MarkdownDescription: "Data size in **bytes**", + }, + }, + Return: function.NumberReturn{}, + } +} + +func (f *ToTerabytesModel) Run(ctx context.Context, req function.RunRequest, resp *function.RunResponse) { + var bytes types.Number + + resp.Error = function.ConcatFuncErrors(resp.Error, req.Arguments.Get(ctx, &bytes)) + resp.Error = function.ConcatFuncErrors(resp.Error, resp.Result.Set(ctx, converter.TerabytesFromBytes(bytes))) +} diff --git a/internal/provider/provider.go b/internal/provider/provider.go index 9038fb6..21c5a0c 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -1,6 +1,8 @@ -// Copyright (c) HashiCorp, Inc. -// Copyright (c) Dmitrii Starov -// SPDX-License-Identifier: MPL-2.0 +/* + * Copyright (c) HashiCorp, Inc. + * Copyright (c) 2024. Dmitry Starov + * SPDX-License-Identifier: MPL-2.0 + */ package provider @@ -14,6 +16,7 @@ import ( "github.com/hashicorp/terraform-plugin-framework/resource" mydatasource "github.com/dstaroff/terraform-provider-units/internal/provider/datasource" + myfuncs "github.com/dstaroff/terraform-provider-units/internal/provider/function" ) var _ provider.Provider = &Units{} @@ -27,12 +30,16 @@ type Units struct { version string } -var unitsDescription = "This provider gives a possibility to use data sources as containers for measurement units and converting them in an interoperable manner." +var unitsDescription = `This provider gives you a possibility to convert categorized units in an interoperable manner. ` + + `Use data sources as containers for measurement units and converting them. ` + + `Or, convert them using provider-defined functions. ` const unitsDescriptionMd = // language=markdown ` -This provider gives a possibility to use data sources as containers for measurement units and converting them in an interoperable manner. +This provider gives you a possibility to convert categorized units in an interoperable manner. +Use [data sources](https://developer.hashicorp.com/terraform/language/data-sources) as containers for measurement units and converting them. +Or, convert them using [provider-defined functions](https://www.hashicorp.com/blog/terraform-1-8-improves-extensibility-with-provider-defined-functions). ## Problem to solve @@ -42,29 +49,45 @@ This provider gives a possibility to use data sources as containers for measurem ` + "```terraform" + ` resource "cloud_provider_disk" "this" { - size = var.disk_size_gib * 1024 * 1024 * 1024 + size = var.disk_size_gib * 1024 * 1024 * 1024 } resource "another_cloud_provider_disk" "that" { - size_gb = ceil((var.disk_size_gib * (1024 * 1024 * 1024)) / (1000 * 1000 * 1000)) + size_gb = ceil((var.disk_size_gib * (1024 * 1024 * 1024)) / (1000 * 1000 * 1000)) } ` + "```" + ` ## Solution -Simply use: +### Data source + +> With data sources, you can store converted values in a container, which will be stored in your state. ` + "```terraform" + ` data "units_data_size" "disk" { - gibibytes = var.disk_size_gib + gibibytes = var.disk_size_gib +} + +resource "cloud_provider_disk" "this" { + size = data.units_data_size.disk.bytes } +resource "another_cloud_provider_disk" "that" { + size_gb = ceil(data.units_data_size.disk.gigabytes) +} +` + "```" + ` + +### Functions + +> Converter function results are being computed during ` + "`plan`" + `, and won't be stored in the state. + +` + "```terraform" + ` resource "cloud_provider_disk" "this" { - size = data.units_data_size.disk.bytes + size = provider::units::from_gib(var.disk_size_gib) } resource "another_cloud_provider_disk" "that" { - size_gb = ceil(data.units_data_size.disk.gigabytes) + size_gb = ceil(provider::units::to_gb(provider::units::from_gib(var.disk_size_gib))) } ` + "```" + ` @@ -112,7 +135,9 @@ func (p *Units) DataSources(_ context.Context) []func() datasource.DataSource { } func (p *Units) Functions(_ context.Context) []func() function.Function { - return []func() function.Function{} + var res []func() function.Function + res = append(res, myfuncs.GeneratedFunctions...) + return res } func New(version string) func() provider.Provider { From a835423187302c544894dc77e13ef92cb6243ebc Mon Sep 17 00:00:00 2001 From: Dmitry Starov Date: Sun, 15 Sep 2024 03:04:09 +0200 Subject: [PATCH 10/11] Update workflows --- .github/workflows/changie.yml | 18 ++++- .github/workflows/release.yml | 55 +++++++++----- .github/workflows/test.yml | 134 +++++++++++++++++++++++----------- 3 files changed, 142 insertions(+), 65 deletions(-) diff --git a/.github/workflows/changie.yml b/.github/workflows/changie.yml index c60b90b..d4cffcc 100644 --- a/.github/workflows/changie.yml +++ b/.github/workflows/changie.yml @@ -1,11 +1,14 @@ -name: Changie +name: Changelog on: pull_request: + branches: + - main paths: - .changes/unreleased/*.yaml - .changie.yaml - .github/workflows/changie.yml + - CHANGELOG.md permissions: contents: read @@ -15,11 +18,18 @@ jobs: name: Ensure no diff runs-on: ubuntu-latest steps: - - name: Check out code + - name: Checkout uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7 - - name: Run Changie + - name: Batch changes + continue-on-error: true uses: miniscruff/changie-action@6dcc2533cac0495148ed4046c438487e4dceaa23 # v2.0.0 with: version: latest - args: batch patch --dry-run + args: batch auto + + - name: Ensure no diff + # language=bash + run: | + git diff --compact-summary --exit-code || \ + (echo "*** Unexpected diff after code generation. Run 'make generate-docs' and commit."; exit 1) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 62e1d3e..6e8bf21 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -1,12 +1,11 @@ name: Release on: - workflow_dispatch: - inputs: - version: - description: 'The current version to be released (vX.Y.Z)' - required: true - type: string + push: + branches: + - main + tags: + - 'v*' concurrency: group: ${{ github.workflow }} @@ -15,33 +14,53 @@ permissions: contents: write jobs: - goreleaser: - name: Release - environment: terraform-registry + get-version: + name: Get version runs-on: ubuntu-latest steps: - - name: Import GPG key - uses: crazy-max/ghaction-import-gpg@01dd5d3ca463c7f10f7f4f7b4f177225ac661ee4 # v6.1.0 - id: import_gpg + - name: Checkout + uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7 with: - gpg_private_key: ${{ secrets.GPG_PRIVATE_KEY }} - passphrase: ${{ secrets.GPG_PASSPHRASE }} + fetch-depth: 0 + + - name: Get the latest version + id: latest + uses: miniscruff/changie-action@6dcc2533cac0495148ed4046c438487e4dceaa23 # v2.0.0 + with: + args: latest + + outputs: + latest: ${{ steps.latest.outputs.output }} - - name: Check out code + goreleaser: + name: Release ${{ needs.get-version.outputs.latest }} + needs: + - get-version + environment: terraform-registry + runs-on: ubuntu-latest + steps: + - name: Checkout uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7 with: fetch-depth: 0 - - name: Set up Go + - name: Setup Go uses: actions/setup-go@0a12ed9d6a96ab950c8f026ed9f722fe0da7ef32 # v5.0.2 with: go-version-file: 'go.mod' cache: true - - name: Run GoReleaser + - name: Import GPG key + uses: crazy-max/ghaction-import-gpg@01dd5d3ca463c7f10f7f4f7b4f177225ac661ee4 # v6.1.0 + id: import-gpg + with: + gpg_private_key: ${{ secrets.GPG_PRIVATE_KEY }} + passphrase: ${{ secrets.GPG_PASSPHRASE }} + + - name: Release uses: goreleaser/goreleaser-action@286f3b13b1b49da4ac219696163fb8c1c93e1200 # v6.0.0 with: args: release --clean env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - GPG_FINGERPRINT: ${{ steps.import_gpg.outputs.fingerprint }} + GPG_FINGERPRINT: ${{ steps.import-gpg.outputs.fingerprint }} diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 190ace9..612316f 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -1,8 +1,9 @@ -name: Tests +name: Test on: pull_request: - branches: [ main ] + branches: + - main paths-ignore: - 'README.md' - 'CHANGELOG.md' @@ -11,65 +12,113 @@ permissions: contents: read jobs: - build: - name: Build + generate: + name: Generate runs-on: ubuntu-latest - timeout-minutes: 5 steps: - - name: Check out code + - name: Checkout uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7 - - name: Set up Go + - name: Setup Go uses: actions/setup-go@0a12ed9d6a96ab950c8f026ed9f722fe0da7ef32 # v5.0.2 with: go-version-file: 'go.mod' cache: true - - name: Run linters - uses: golangci/golangci-lint-action@aaa42aa0628b4ae2578232a66b541047968fac86 # v6.1.0 + - name: Generate sources + # language=bash + run: | + make generate + + - name: Ensure no diff + # language=bash + run: | + git diff --compact-summary --exit-code || \ + (echo "*** Unexpected diff after code generation. Run 'make generate' and commit."; exit 1) + + - name: Setup Terraform + uses: hashicorp/setup-terraform@b9cd54a3c349d3f38e8881555d616ced269862dd # v3.1.2 with: - version: latest + terraform_version: '1.9.4' + terraform_wrapper: false - - name: Build - # language=shell script + - name: Generate docs + # language=bash run: | - make build + make generate-docs - generate: - name: Check docs + - name: Ensure no diff + # language=bash + run: | + git diff --compact-summary --exit-code || \ + (echo "*** Unexpected diff after code generation. Run 'make generate-docs' and commit."; exit 1) + + lint: + name: Lint + needs: + - generate runs-on: ubuntu-latest steps: - - name: Check out code + - name: Checkout uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7 - - name: Set up Go + - name: Setup Go uses: actions/setup-go@0a12ed9d6a96ab950c8f026ed9f722fe0da7ef32 # v5.0.2 with: go-version-file: 'go.mod' cache: true - - name: Set up Terraform - uses: hashicorp/setup-terraform@b9cd54a3c349d3f38e8881555d616ced269862dd # v3.1.2 + - name: Lint + uses: golangci/golangci-lint-action@aaa42aa0628b4ae2578232a66b541047968fac86 # v6.1.0 with: - terraform_version: '1.9.4' - terraform_wrapper: false + version: latest - - name: Generate docs - # language=shell script + test-unit: + name: Test [unit] + needs: + - generate + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7 + + - name: Setup Go + uses: actions/setup-go@0a12ed9d6a96ab950c8f026ed9f722fe0da7ef32 # v5.0.2 + with: + go-version-file: 'go.mod' + cache: true + + - name: Test + # language=bash run: | - make generate + make test - - name: Ensure no diff - # language=shell script + build: + name: Build + needs: + - lint + - test-unit + runs-on: ubuntu-latest + timeout-minutes: 5 + steps: + - name: Checkout + uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7 + + - name: Setup Go + uses: actions/setup-go@0a12ed9d6a96ab950c8f026ed9f722fe0da7ef32 # v5.0.2 + with: + go-version-file: 'go.mod' + cache: true + + - name: Build + # language=bash run: | - git diff --compact-summary --exit-code || \ - (echo "*** Unexpected diff after code generation. Run 'make generate' and commit."; exit 1) + make build - test: - name: 'AccTest [OS: ${{ matrix.os }} | TF: ${{ matrix.terraform }}]' + test-acceptance: + name: 'Test [Acc ${{ matrix.os-short }}:${{ matrix.terraform }}]' needs: - build - - generate runs-on: ${{ matrix.os }} timeout-minutes: 15 strategy: @@ -80,33 +129,32 @@ jobs: - windows-latest - ubuntu-latest terraform: - - '1.0.*' - - '1.1.*' - - '1.2.*' - - '1.3.*' - - '1.4.*' - - '1.5.*' - - '1.6.*' - - '1.7.*' - '1.8.*' - '1.9.*' + include: + - os: macos-latest + os-short: macos + - os: windows-latest + os-short: windows + - os: ubuntu-latest + os-short: ubuntu steps: - - name: Check out code + - name: Checkout uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7 - - name: Set up Go + - name: Setup Go uses: actions/setup-go@0a12ed9d6a96ab950c8f026ed9f722fe0da7ef32 # v5.0.2 with: go-version-file: 'go.mod' cache: true - - name: Set up Terraform + - name: Setup Terraform uses: hashicorp/setup-terraform@b9cd54a3c349d3f38e8881555d616ced269862dd # v3.1.2 with: terraform_version: ${{ matrix.terraform }} terraform_wrapper: false - name: Run acceptance test - # language=shell script + # language=bash run: | make test-acceptance From 73f585dd087bb3c4ed9a5ab886ce4e5706133186 Mon Sep 17 00:00:00 2001 From: Dmitry Starov Date: Sun, 15 Sep 2024 03:06:14 +0200 Subject: [PATCH 11/11] Bump version to `v1.0.0` --- .../unreleased/Added-20240915-030145.yaml | 3 --- .../unreleased/Enhanced-20240915-025916.yaml | 3 --- .../unreleased/Fixed-20240915-030024.yaml | 3 --- .../unreleased/Security-20240915-025743.yaml | 3 --- .changes/v1.0.0.md | 17 +++++++++++++++++ CHANGELOG.md | 19 +++++++++++++++++++ 6 files changed, 36 insertions(+), 12 deletions(-) delete mode 100644 .changes/unreleased/Added-20240915-030145.yaml delete mode 100644 .changes/unreleased/Enhanced-20240915-025916.yaml delete mode 100644 .changes/unreleased/Fixed-20240915-030024.yaml delete mode 100644 .changes/unreleased/Security-20240915-025743.yaml create mode 100644 .changes/v1.0.0.md diff --git a/.changes/unreleased/Added-20240915-030145.yaml b/.changes/unreleased/Added-20240915-030145.yaml deleted file mode 100644 index 102aa54..0000000 --- a/.changes/unreleased/Added-20240915-030145.yaml +++ /dev/null @@ -1,3 +0,0 @@ -kind: Added -body: Provider-defined functions to convert values without storing in the state. -time: 2024-09-15T03:01:45.6714822+02:00 diff --git a/.changes/unreleased/Enhanced-20240915-025916.yaml b/.changes/unreleased/Enhanced-20240915-025916.yaml deleted file mode 100644 index 04cbe13..0000000 --- a/.changes/unreleased/Enhanced-20240915-025916.yaml +++ /dev/null @@ -1,3 +0,0 @@ -kind: Enhanced -body: Added EditorConfig. -time: 2024-09-15T02:59:16.5811877+02:00 diff --git a/.changes/unreleased/Fixed-20240915-030024.yaml b/.changes/unreleased/Fixed-20240915-030024.yaml deleted file mode 100644 index f64fead..0000000 --- a/.changes/unreleased/Fixed-20240915-030024.yaml +++ /dev/null @@ -1,3 +0,0 @@ -kind: Fixed -body: CI tool configs. -time: 2024-09-15T03:00:24.8989296+02:00 diff --git a/.changes/unreleased/Security-20240915-025743.yaml b/.changes/unreleased/Security-20240915-025743.yaml deleted file mode 100644 index 6ca7530..0000000 --- a/.changes/unreleased/Security-20240915-025743.yaml +++ /dev/null @@ -1,3 +0,0 @@ -kind: Security -body: Updated dependency versions for doc generator. -time: 2024-09-15T02:57:43.8201012+02:00 diff --git a/.changes/v1.0.0.md b/.changes/v1.0.0.md new file mode 100644 index 0000000..9ed31ff --- /dev/null +++ b/.changes/v1.0.0.md @@ -0,0 +1,17 @@ +## v1.0.0 - 2024-09-15 + +### Added + +* Provider-defined functions to convert values without storing in the state. + +### Enhanced + +* Added EditorConfig. + +### Fixed + +* CI tool configs. + +### Security + +* Updated dependency versions for doc generator. diff --git a/CHANGELOG.md b/CHANGELOG.md index 164b8f0..e57c6c2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,22 @@ +## v1.0.0 - 2024-09-15 + +### Added + +* Provider-defined functions to convert values without storing in the state. + +### Enhanced + +* Added EditorConfig. + +### Fixed + +* CI tool configs. + +### Security + +* Updated dependency versions for doc generator. + + ## v0.2.0 - 2024-09-15 ### Enhanced