|
| 1 | +:lastproofread: 2024-03-10 |
| 2 | + |
| 3 | +.. _vyos-govyos: |
| 4 | + |
| 5 | +go-vyos |
| 6 | +======= |
| 7 | + |
| 8 | +go-vyos is a Go library designed for interacting with VyOS devices through |
| 9 | +their REST API. This documentation is intended to guide you in using go-vyos for |
| 10 | +programmatic management of your VyOS devices. |
| 11 | + |
| 12 | +- `go-vyos Documentation & Source Code on GitHub <https://github.com/ganawaj/go-vyos>`_ |
| 13 | + allows you to access and contribute to the library's code. |
| 14 | +- `go-vyos on pkg.go.dev <https://pkg.go.dev/github.com/ganawaj/go-vyos@v0.1.0/vyos>`_ for detailed instructions |
| 15 | + on the installation, configuration, and operation of the go-vyos library. |
| 16 | + |
| 17 | + |
| 18 | +Installation |
| 19 | +------------ |
| 20 | + |
| 21 | +You can install go-vyos: |
| 22 | + |
| 23 | +.. code-block:: bash |
| 24 | +
|
| 25 | + go install "github.com/ganawaj/go-vyos/vyos" |
| 26 | +
|
| 27 | +Getting Started |
| 28 | +--------------- |
| 29 | + |
| 30 | +Importing and Disabling TLS Verification |
| 31 | +------------------------------------------------- |
| 32 | + |
| 33 | +.. code-block:: none |
| 34 | +
|
| 35 | + import "github.com/ganawaj/go-vyos/vyos" |
| 36 | + client := vyos.NewClient(nil).WithToken("AUTH_KEY").WithURL("https://192.168.0.1").Insecure() |
| 37 | +
|
| 38 | +Initializing a VyDevice Object |
| 39 | +------------------------------ |
| 40 | + |
| 41 | +.. code-block:: none |
| 42 | +
|
| 43 | + import ( |
| 44 | + "github.com/ganawaj/go-vyos/vyos" |
| 45 | + "os" |
| 46 | + ) |
| 47 | +
|
| 48 | + hostname := os.Getenv('VYDEVICE_HOSTNAME') |
| 49 | + port := os.Getenv('VYDEVICE_PORT') |
| 50 | + url := fmt.Sprintf("https://%s:%s", hostname, port) |
| 51 | +
|
| 52 | + apikey := os.Getenv('VYDEVICE_APIKEY') |
| 53 | + verify_ssl := os.Getenv('VYDEVICE_VERIFY_SSL') |
| 54 | +
|
| 55 | + client := vyos.NewClient(nil).WithToken(apikey).WithURL(url) |
| 56 | +
|
| 57 | + if verify_ssl == "false" { |
| 58 | + client = client.Insecure() |
| 59 | + } |
| 60 | +
|
| 61 | +Using go-vyos |
| 62 | +---------------- |
| 63 | + |
| 64 | +Configure, then Set |
| 65 | +^^^^^^^^^^^^^^^^^^^^^^^^ |
| 66 | + |
| 67 | +.. code-block:: none |
| 68 | +
|
| 69 | + out, resp, err := c.Conf.Set(ctx, "interfaces ethernet eth0 address 192.168.1.1/24") |
| 70 | + if err != nil { |
| 71 | + panic("Error: %v", err) |
| 72 | + } |
| 73 | +
|
| 74 | + fmt.Println(out.Success) |
| 75 | +
|
| 76 | +Show a Single Object Value |
| 77 | +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 78 | + |
| 79 | +.. code-block:: none |
| 80 | +
|
| 81 | + out, resp, err := c.Show.Do(ctx, "interfaces dummy dum1 address") |
| 82 | + if err != nil { |
| 83 | + panic("Error: %v", err) |
| 84 | + } |
| 85 | +
|
| 86 | + fmt.Println(out.Success) |
| 87 | + fmt.Printf("Data: %v\n", out.Data) |
| 88 | +
|
| 89 | +Configure, then Show Object |
| 90 | +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 91 | + |
| 92 | +.. code-block:: none |
| 93 | +
|
| 94 | + out, resp, err := c.Conf.Get(ctx, "interfaces dummy dum1", nil) |
| 95 | + if err != nil { |
| 96 | + panic("Error: %v", err) |
| 97 | + } |
| 98 | +
|
| 99 | + fmt.Println(out.Success) |
| 100 | + fmt.Printf("Data: %v\n", out.Data) |
| 101 | +
|
| 102 | +Configure, then Show Multivalue Object |
| 103 | +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 104 | + |
| 105 | +.. code-block:: none |
| 106 | +
|
| 107 | + options := RetrieveOptions{ |
| 108 | + Multivalue: true, |
| 109 | + } |
| 110 | +
|
| 111 | + out, resp, err := c.Conf.Get(ctx, "interfaces dummy dum1", options) |
| 112 | + if err != nil { |
| 113 | + panic("Error: %v", err) |
| 114 | + } |
| 115 | +
|
| 116 | + fmt.Println(out.Success) |
| 117 | +
|
| 118 | +
|
| 119 | +Configure, then Delete Object |
| 120 | +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 121 | + |
| 122 | +.. code-block:: none |
| 123 | +
|
| 124 | + out, resp, err := c.Conf.Delete(ctx, "interfaces dummy dum1") |
| 125 | + if err != nil { |
| 126 | + panic("Error: %v", err) |
| 127 | + } |
| 128 | +
|
| 129 | + fmt.Println(out.Success) |
| 130 | +
|
| 131 | +Configure, then Save |
| 132 | +^^^^^^^^^^^^^^^^^^^^^^^^ |
| 133 | + |
| 134 | +.. code-block:: none |
| 135 | +
|
| 136 | + out, resp, err := c.Conf.Save(ctx, "") |
| 137 | +
|
| 138 | + if err != nil { |
| 139 | + panic("Error: %v", err) |
| 140 | + } |
| 141 | +
|
| 142 | + fmt.Println(out.Success) |
| 143 | +
|
| 144 | +Configure, then Save File |
| 145 | +------------------------- |
| 146 | + |
| 147 | +.. code-block:: none |
| 148 | +
|
| 149 | + out, resp, err := c.Conf.Save(ctx, "/config/test300.config") |
| 150 | +
|
| 151 | + if err != nil { |
| 152 | + panic("Error: %v", err) |
| 153 | + } |
| 154 | +
|
| 155 | + fmt.Println(out.Success) |
| 156 | +
|
| 157 | +Show Object |
| 158 | +^^^^^^^^^^^^^^ |
| 159 | + |
| 160 | +.. code-block:: none |
| 161 | +
|
| 162 | + out, resp, err := c.Show.Do(ctx, "system image") |
| 163 | + if err != nil { |
| 164 | + panic("Error: %v", err) |
| 165 | + } |
| 166 | +
|
| 167 | + fmt.Println(out.Success) |
| 168 | + fmt.Printf("Data: %v\n", out.Data) |
| 169 | +
|
| 170 | +Generate Object |
| 171 | +^^^^^^^^^^^^^^^^ |
| 172 | + |
| 173 | +.. code-block:: none |
| 174 | +
|
| 175 | + out, resp, err := c.Generate.Do(ctx, "pki wireguard key-pair") |
| 176 | + if err != nil { |
| 177 | + panic("Error: %v", err) |
| 178 | + } |
| 179 | +
|
| 180 | + fmt.Println(out.Success) |
| 181 | + fmt.Printf("Data: %v\n", out.Data) |
| 182 | +
|
| 183 | +Reset Object |
| 184 | +^^^^^^^^^^^^^^ |
| 185 | + |
| 186 | +.. code-block:: none |
| 187 | +
|
| 188 | + out, resp, err := c.Reset.Do(ctx, "ip bgp 192.0.2.11") |
| 189 | + if err != nil { |
| 190 | + panic("Error: %v", err) |
| 191 | + } |
| 192 | +
|
| 193 | + fmt.Println(out.Success) |
| 194 | + fmt.Printf("Data: %v\n", out.Data) |
| 195 | +
|
| 196 | +Configure, then Load File |
| 197 | +^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 198 | + |
| 199 | +.. code-block:: none |
| 200 | +
|
| 201 | + out, resp, err := c.ConfigFile.Load(ctx, "/config/test300.config") |
| 202 | +
|
| 203 | +.. _go-vyos: https://github.com/ganawaj/go-vyos |
0 commit comments