Skip to content

Commit

Permalink
Merge pull request #35 from aaronchar/feat/migrate-netconf-helper
Browse files Browse the repository at this point in the history
Migrate NETCONF junos_helper to provider.
  • Loading branch information
aburston authored Jul 26, 2023
2 parents 7f09d54 + e04e78c commit a8a16c2
Show file tree
Hide file tree
Showing 6 changed files with 362 additions and 51 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// Copyright (c) 2017-2022, Juniper Networks Inc. All rights reserved.
package processProviders

const providerConfigContent = `// Copyright (c) 2017-2022, Juniper Networks Inc. All rights reserved.
//
// License: Apache 2.0
//
Expand All @@ -16,11 +18,11 @@
package main
import (
gonetconf "github.com/davedotdev/go-netconf/helpers/junos_helpers"
import(
"terraform-provider-junos-%+v/netconf"
)
// Config is the configuration structure used to instantiate the GoNETCONF provider.
// Config is the configuration structure used to instantiate the Netconf provider.
type Config struct {
Host string
Port int
Expand All @@ -30,12 +32,12 @@ type Config struct {
}
// Client returns a new client for the provider to use
func (c *Config) Client() (*gonetconf.GoNCClient, error) {
func (c *Config) Client() (netconf.Client, error) {
return newClient(c)
}
func newClient(c *Config) (*gonetconf.GoNCClient, error) {
func newClient(c *Config) (netconf.Client, error) {
client, err := gonetconf.NewClient(c.Username, c.Password, c.SSHKey, c.Host, c.Port)
client, err := netconf.NewClient(c.Username, c.Password, c.SSHKey, c.Host, c.Port)
return client, err
}
}`
93 changes: 66 additions & 27 deletions Internal/processProviders/processProviders.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,18 @@ import (
"bytes"
"encoding/xml"
"fmt"
"io"
"io/fs"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"regexp"
"strconv"
"strings"
s "strings"
"sync/atomic"
"unicode/utf8"

"github.com/Juniper/junos-terraform/Internal/cfg"
Expand Down Expand Up @@ -284,7 +288,7 @@ func CreateProviders(jcfg cfg.Config) error {
check(err)

// Write to the file
_, err = fPtr.WriteString(providerFileData)
_, err = fPtr.WriteString(fmt.Sprintf(providerFileData, jcfg.ProviderName))

// List summary data
fmt.Println("--------------------------------------------------------------------------------")
Expand All @@ -307,37 +311,27 @@ func CreateProviders(jcfg cfg.Config) error {
tpPath += "/"
}
}

// Copy the files from ../terraform_providers to the `providerDir` from the config file
files, err := ioutil.ReadDir(tpPath)
if err != nil {
fmt.Println(err)
}

fmt.Println()
PrintHeader("Copying the rest of the required Go files")
for _, f := range files {
if strings.Contains(f.Name(), ".go") {
input, err := ioutil.ReadFile(tpPath + "/" + f.Name())
if err != nil {
fmt.Println(err)
}

err = ioutil.WriteFile(jcfg.ProviderDir+"/"+f.Name(), input, 0644)
if err != nil {
fmt.Println("Error creating", jcfg.ProviderDir+"/"+f.Name())
}

fmt.Printf("Copied file: %+v to %+v\n", f.Name(), jcfg.ProviderDir)
}
// Copy the go files from ../terraform_providers to the `providerDir` from the config file
PrintHeader("Copying files")
var fileCopyCount uint32
if err := copyDir(tpPath, jcfg.ProviderDir, ".go", &fileCopyCount); err != nil {
return fmt.Errorf("failed to copy files from the %s direcotry: %w", tpPath, err)
}
fmt.Println("------------------------------------------------------------")
fmt.Println(fmt.Sprintf("- Copied a total of %d .go files from %s to %s -", fileCopyCount, filepath.Base(tpPath), filepath.Base(jcfg.ProviderDir)))
fmt.Println("------------------------------------------------------------")

PrintHeader("Creating Go Mod")
err = ioutil.WriteFile(jcfg.ProviderDir+"/go.mod", []byte(fmt.Sprintf(gomodcontent, jcfg.ProviderName)), 0644)
if err != nil {
fmt.Println("Error creating", jcfg.ProviderDir+"/go.mod")
}

PrintHeader("Creating provider config")
err = ioutil.WriteFile(jcfg.ProviderDir+"/config.go", []byte(fmt.Sprintf(providerConfigContent, jcfg.ProviderName)), 0644)
if err != nil {
fmt.Println("Error creating", jcfg.ProviderDir+"/config.go")
}
// No errors, so return nil.
return nil
}
Expand Down Expand Up @@ -421,7 +415,7 @@ import (
strUpdate = ""

strDelete = `
_, err = client.DeleteConfigNoCommit(id)
_, err = client.DeleteConfig(id,false)
check(ctx, err)
d.SetId("")
Expand Down Expand Up @@ -1140,16 +1134,16 @@ package main
import (
"context"
gonetconf "github.com/davedotdev/go-netconf/helpers/junos_helpers"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"terraform-provider-junos-%+v/netconf"
"os"
)
// ProviderConfig is to hold client information
type ProviderConfig struct {
*gonetconf.GoNCClient
netconf.Client
Host string
}
Expand Down Expand Up @@ -1219,3 +1213,48 @@ func Provider() *schema.Provider {
ResourcesMap: map[string]*schema.Resource{
`
}

func copyDir(src, dst, extension string, fileCopyCount *uint32) error {
return filepath.Walk(src, func(path string, info fs.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
if !strings.Contains(info.Name(), extension) {
return nil
}
atomic.AddUint32(fileCopyCount, 1)
}
fmt.Println(fmt.Sprintf("- Copying %s to %s", info.Name(), filepath.Base(dst)))
outpath := filepath.Join(dst, strings.TrimPrefix(path, src))
if info.IsDir() {
os.MkdirAll(outpath, info.Mode())
return nil // means recursive
}
if !info.Mode().IsRegular() {
switch info.Mode().Type() & os.ModeType {
case os.ModeSymlink:
link, err := os.Readlink(path)
if err != nil {
return err
}
return os.Symlink(link, outpath)
}
return nil
}
in, _ := os.Open(path)
if err != nil {
return err
}
defer in.Close()
fh, err := os.Create(outpath)
if err != nil {
return err
}
defer fh.Close()

fh.Chmod(info.Mode())
_, err = io.Copy(fh, in)
return err
})
}
9 changes: 9 additions & 0 deletions terraform_providers/netconf/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package netconf

type Client interface {
Close() error
DeleteConfig(applyGroup string, commit bool) (string, error)
SendCommit() error
MarshalGroup(id string, obj interface{}) error
SendTransaction(id string, obj interface{}, commit bool) error
}
Loading

0 comments on commit a8a16c2

Please sign in to comment.