Skip to content

Commit

Permalink
Alpha3 (#6)
Browse files Browse the repository at this point in the history
* check file exists

* terraform examples

* fix initial file creation
  • Loading branch information
cdalar authored Nov 27, 2023
1 parent b2043eb commit 830cde3
Show file tree
Hide file tree
Showing 17 changed files with 197,206 additions and 26 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
parampiper
.terraform
.terraform.lock.hcl
terraform.tfstate
terraform.tfstate.backup
pp.auto.tfvars
37 changes: 37 additions & 0 deletions cmd/out.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package cmd

import (
"log"

"github.com/spf13/cobra"
)

func init() {
outCmd.Flags().StringVarP(&outputType, "output", "o", "", "Output type: raw, json, yaml, table")
rootCmd.AddCommand(outCmd)
}

var outCmd = &cobra.Command{
Use: "out",
Aliases: []string{"out"},
Short: "Output Parameters",
Run: func(cmd *cobra.Command, args []string) {
var tmpl string
log.Println("[DEBUG] Output Parameters")

parameters, err := provider.Read()
if err != nil {
log.Println(err)
}

log.Println("[DEBUG]", parameters)
switch outputType {
case "tfvars":
tmpl = "{{range .}}{{.Name}} = \"{{.Value}}\"\n{{end}}"
default:
tmpl = "NAME\tVALUE\tINFO\n{{range .}}{{.Name}}\t{{.Value}}\t{{.Info}}\n{{end}}"
}

TabWriter(parameters, tmpl)
},
}
3 changes: 1 addition & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cmd

import (
"fmt"
"log"
"os"
"strings"
Expand Down Expand Up @@ -56,7 +55,7 @@ func Execute() error {

func checkDataProvider() {
dataProvider := os.Getenv("PP_DATA")
fmt.Println("Using: " + dataProvider)
log.Println("[DEBUG] Using: " + dataProvider)
if dataProvider != "" {
if !Contains(providerList, dataProvider) {
log.Println("Provider (" + dataProvider + ") is not Supported\nPlease use one of the following: " + strings.Join(providerList, ","))
Expand Down
4 changes: 2 additions & 2 deletions cmd/add.go → cmd/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ func init() {
}

var addCmd = &cobra.Command{
Use: "put",
Aliases: []string{"add", "put"},
Use: "set",
Aliases: []string{"set", "put"},
Short: "Add/Update Parameter",
Run: func(cmd *cobra.Command, args []string) {
log.Println("[DEBUG] Add/Update Parameters")
Expand Down
17 changes: 15 additions & 2 deletions internal/data/azure_blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/bloberror"
)

type AzureStorageAccount struct {
Expand Down Expand Up @@ -34,8 +35,20 @@ func (p AzureStorageAccount) Read() (Parameters, error) {

log.Println("[DEBUG]", "SA Name:", p.StorageAccountName, "\nContainer Name:", p.ContainerName, "\nFileName:", p.BlobName)
props, err := blobClient.GetProperties(ctx, nil)
if err != nil {
log.Fatalf("Error getting properties: %v", err)
if bloberror.HasCode(err, bloberror.BlobNotFound) {
log.Println("[DEBUG] Blob not found:", err)
_, err = client.UploadBuffer(ctx, p.ContainerName, p.BlobName, []byte("[]"), &azblob.UploadBufferOptions{})
if err != nil {
log.Fatalf("Error on creating empty blob: %v", err)
}

props, err = blobClient.GetProperties(ctx, nil)
if err != nil {
log.Println(err)
}
} else if err != nil {
log.Fatalf("Error getting blob properties: %v", err)

}

blobSize := props.ContentLength
Expand Down
27 changes: 20 additions & 7 deletions internal/data/local_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,29 @@ type LocalFile struct {
FilePath string
}

func fileExists(filename string) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}

func (p LocalFile) Read() (Parameters, error) {
log.Println("[DEBUG] Reading from LocalFile")
jsonBlob, err := os.ReadFile(p.FilePath)
if err != nil {
log.Println(err)
}
parameters := Parameters{}
err = json.Unmarshal(jsonBlob, &parameters)
if err != nil {
log.Println(err)
if fileExists(p.FilePath) {
jsonBlob, err := os.ReadFile(p.FilePath)
if err != nil {
log.Println(err)
}
err = json.Unmarshal(jsonBlob, &parameters)
if err != nil {
log.Println(err)
}
} else {
fmt.Println(p.FilePath, "file does not exist")
os.Exit(1)
}
log.Println("[DEBUG]", parameters)
return parameters, nil
Expand Down
25 changes: 12 additions & 13 deletions parampiper.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,36 @@
{
"name": "VNET22",
"value": "virtualNetworks/test-vnet",
"info": "",
"time": ""
"info": ""
},
{
"name": "SA_DEV_CONT",
"value": "/stparampiper",
"info": "",
"time": ""
"info": ""
},
{
"name": "SA_DEV_CONT33",
"value": "/subscriptions/xxx/resourceGroups/",
"info": "",
"time": ""
"info": ""
},
{
"name": "testName22",
"value": "testValue",
"info": "",
"time": ""
"info": ""
},
{
"name": "testName567",
"value": "testValue",
"info": "",
"time": ""
"value": "433",
"info": ""
},
{
"name": "cemal22222",
"value": "43",
"info": "infoede",
"time": ""
"info": "infoede"
},
{
"name": "cemal",
"value": "45622",
"info": ""
}
]
16 changes: 16 additions & 0 deletions tests/terraform/example-pl1/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

provider "azurerm" {
features {}
}

resource "azurerm_resource_group" "example" {
name = var.resource_group_name
location = var.location
}

resource "azurerm_virtual_network" "example" {
name = "example-vnet"
address_space = ["10.0.0.0/16"]
location = azurerm_resource_group.example.location
resource_group_name = azurerm_resource_group.example.name
}
4 changes: 4 additions & 0 deletions tests/terraform/example-pl1/output.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

output "vnet_id" {
value = azurerm_virtual_network.example.id
}
2 changes: 2 additions & 0 deletions tests/terraform/example-pl1/parameters.tfvars
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
resource_group_name = "rg-pp-tf-example"
location = "westeurope"
Loading

0 comments on commit 830cde3

Please sign in to comment.