summaryrefslogtreecommitdiff
path: root/docs/automation/vyos-govyos.rst
blob: 0534235f77453103683a7c104ef4a18e6982f857 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
:lastproofread: 2024-03-10

.. _vyos-govyos:

go-vyos
=======

go-vyos is a Go library designed for interacting with VyOS devices through
their API. This documentation is intended to guide you in using go-vyos for
programmatic management of your VyOS devices.

- `go-vyos Documentation & Source Code on GitHub <https://github.com/ganawaj/go-vyos>`_
  allows you to access and contribute to the library's code.
- `go-vyos on pkg.go.dev <https://pkg.go.dev/github.com/ganawaj/go-vyos@v0.1.0/vyos>`_ for detailed instructions
  on the installation, configuration, and operation of the go-vyos library.


Installation
------------

You can install go-vyos:

.. code-block:: bash

    go install "github.com/ganawaj/go-vyos/vyos"

Getting Started
---------------

Importing and Disabling TLS Verification
-------------------------------------------------

.. code-block:: none

    import "github.com/ganawaj/go-vyos/vyos"
    client := vyos.NewClient(nil).WithToken("AUTH_KEY").WithURL("https://192.168.0.1").Insecure()

Initializing a VyDevice Object
------------------------------

.. code-block:: none

    import (
      "github.com/ganawaj/go-vyos/vyos"
      "os"
    )

    hostname := os.Getenv('VYDEVICE_HOSTNAME')
    port := os.Getenv('VYDEVICE_PORT')
    url := fmt.Sprintf("https://%s:%s", hostname, port)

    apikey := os.Getenv('VYDEVICE_APIKEY')
    verify_ssl := os.Getenv('VYDEVICE_VERIFY_SSL')

    client := vyos.NewClient(nil).WithToken(apikey).WithURL(url)

    if verify_ssl == "false" {
      client = client.Insecure()
    }

Using go-vyos
----------------

Configure, then Set
^^^^^^^^^^^^^^^^^^^^^^^^

.. code-block:: none

    out, resp, err := c.Conf.Set(ctx, "interfaces ethernet eth0 address 192.168.1.1/24")
    if err != nil {
        panic("Error: %v", err)
    }

    fmt.Println(out.Success)

Show a Single Object Value
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

.. code-block:: none

    out, resp, err := c.Show.Do(ctx, "interfaces dummy dum1 address")
    if err != nil {
        panic("Error: %v", err)
    }

    fmt.Println(out.Success)
    fmt.Printf("Data: %v\n", out.Data)

Configure, then Show Object
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

.. code-block:: none

    out, resp, err := c.Conf.Get(ctx, "interfaces dummy dum1", nil)
    if err != nil {
        panic("Error: %v", err)
    }

    fmt.Println(out.Success)
    fmt.Printf("Data: %v\n", out.Data)

Configure, then Show Multivalue Object
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

.. code-block:: none

    options := RetrieveOptions{
        Multivalue: true,
    }

    out, resp, err := c.Conf.Get(ctx, "interfaces dummy dum1", options)
    if err != nil {
        panic("Error: %v", err)
    }

    fmt.Println(out.Success)


Configure, then Delete Object
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

.. code-block:: none

    out, resp, err := c.Conf.Delete(ctx, "interfaces dummy dum1")
    if err != nil {
        panic("Error: %v", err)
    }

    fmt.Println(out.Success)

Configure, then Save
^^^^^^^^^^^^^^^^^^^^^^^^

.. code-block:: none

    out, resp, err := c.Conf.Save(ctx, "")

    if err != nil {
        panic("Error: %v", err)
    }

    fmt.Println(out.Success)

Configure, then Save File
-------------------------

.. code-block:: none

    out, resp, err := c.Conf.Save(ctx, "/config/test300.config")

    if err != nil {
        panic("Error: %v", err)
    }

    fmt.Println(out.Success)

Show Object
^^^^^^^^^^^^^^

.. code-block:: none

    out, resp, err := c.Show.Do(ctx, "system image")
    if err != nil {
        panic("Error: %v", err)
    }

    fmt.Println(out.Success)
    fmt.Printf("Data: %v\n", out.Data)

Generate Object
^^^^^^^^^^^^^^^^

.. code-block:: none

    out, resp, err := c.Generate.Do(ctx, "pki wireguard key-pair")
    if err != nil {
        panic("Error: %v", err)
    }

    fmt.Println(out.Success)
    fmt.Printf("Data: %v\n", out.Data)

Reset Object
^^^^^^^^^^^^^^

.. code-block:: none

    out, resp, err := c.Reset.Do(ctx, "ip bgp 192.0.2.11")
    if err != nil {
        panic("Error: %v", err)
    }

    fmt.Println(out.Success)
    fmt.Printf("Data: %v\n", out.Data)

Configure, then Load File
^^^^^^^^^^^^^^^^^^^^^^^^^^^^

.. code-block:: none

    out, resp, err := c.ConfigFile.Load(ctx, "/config/test300.config")

.. _go-vyos: https://github.com/ganawaj/go-vyos