Skip to content

Commit b1ddebe

Browse files
author
j-griffith
committed
Add json tags to Connector and Secrets
This change adds json tags to the Connector struct and the Secret struct in the iscsi lib. This enables us to add the ability to persist a Connection into a json file, which can then be used to easily rebuild a Connector for Disconnect, or other DR purposes.
1 parent f05aea3 commit b1ddebe

File tree

2 files changed

+51
-19
lines changed

2 files changed

+51
-19
lines changed

iscsi/iscsi.go

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package iscsi
22

33
import (
4+
"encoding/json"
45
"fmt"
56
"io"
67
"io/ioutil"
@@ -33,20 +34,18 @@ type iscsiSession struct {
3334

3435
//Connector provides a struct to hold all of the needed parameters to make our iscsi connection
3536
type Connector struct {
36-
VolumeName string
37-
TargetIqn string
38-
TargetPortals []string
39-
Port string
40-
Lun int32
41-
AuthType string
42-
DiscoverySecrets Secrets
43-
SessionSecrets Secrets
44-
Interface string
45-
Multipath bool
46-
// Number of time we try to obtain a device path, with CheckInterval seconds between each attempt
47-
RetryCount int32
48-
// Time (in seconds) between login attempts
49-
CheckInterval int32
37+
VolumeName string `json:"volume_name"`
38+
TargetIqn string `json:"target_iqn"`
39+
TargetPortals []string `json:"target_portals"`
40+
Port string `json:"port"`
41+
Lun int32 `json:"lun"`
42+
AuthType string `json:"auth_type"`
43+
DiscoverySecrets Secrets `json:"discovery_secrets"`
44+
SessionSecrets Secrets `json:"session_secrets"`
45+
Interface string `json:"interface"`
46+
Multipath bool `json:"multipath"`
47+
RetryCount int32 `json:"retry_count"`
48+
CheckInterval int32 `json:"check_interval"`
5049
}
5150

5251
func init() {
@@ -324,3 +323,36 @@ func Disconnect(tgtIqn string, portals []string) error {
324323
err = DeleteDBEntry(tgtIqn)
325324
return err
326325
}
326+
327+
// PersistConnector persists the provided Connector to the specified file (ie /var/lib/pfile/myConnector.json)
328+
func PersistConnector(c *Connector, filePath string) error {
329+
//file := path.Join("mnt", c.VolumeName+".json")
330+
f, err := os.Create(filePath)
331+
if err != nil {
332+
return fmt.Errorf("error creating iscsi persistence file %s: %s", filePath, err)
333+
}
334+
defer f.Close()
335+
encoder := json.NewEncoder(f)
336+
if err = encoder.Encode(c); err != nil {
337+
return fmt.Errorf("error encoding connector: %v", err)
338+
}
339+
return nil
340+
341+
}
342+
343+
// GetConnectorFromFile attempts to create a Connector using the specified json file (ie /var/lib/pfile/myConnector.json)
344+
func GetConnectorFromFile(filePath string) (*Connector, error) {
345+
f, err := ioutil.ReadFile(filePath)
346+
if err != nil {
347+
return &Connector{}, err
348+
349+
}
350+
data := Connector{}
351+
err = json.Unmarshal([]byte(f), &data)
352+
if err != nil {
353+
return &Connector{}, err
354+
}
355+
356+
return &data, nil
357+
358+
}

iscsi/iscsiadm.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ import (
1111
// Secrets provides optional iscsi security credentials (CHAP settings)
1212
type Secrets struct {
1313
// SecretsType is the type of Secrets being utilized (currently we only impleemnent "chap"
14-
SecretsType string
14+
SecretsType string `json:"secretsType,omitempty"`
1515
// UserName is the configured iscsi user login
16-
UserName string
16+
UserName string `json:"userName"`
1717
// Password is the configured iscsi password
18-
Password string
18+
Password string `json:"password"`
1919
// UserNameIn provides a specific input login for directional CHAP configurations
20-
UserNameIn string
20+
UserNameIn string `json:"userNameIn,omitempty"`
2121
// PasswordIn provides a specific input password for directional CHAP configurations
22-
PasswordIn string
22+
PasswordIn string `json:"passwordIn,omitempty"`
2323
}
2424

2525
// CmdError is a custom error to provide details including the command, stderr output and exit code.

0 commit comments

Comments
 (0)