-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from MATRIXXSoftware/feature/configurable-options
Feature/configurable options
- Loading branch information
Showing
5 changed files
with
188 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
package diameter | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"time" | ||
) | ||
|
||
type DiameterConfig struct { | ||
RequestTimeout *Duration `json:"requestTimeout,omitempty"` | ||
MaxRetransmits *uint `json:"maxRetransmits,omitempty"` | ||
RetransmitInterval *Duration `json:"retransmitInterval,omitempty"` | ||
EnableWatchdog *bool `json:"enableWatchdog,omitempty"` | ||
WatchdogInterval *Duration `json:"watchdogInterval,omitempty"` | ||
WatchdogStream *uint `json:"watchdogStream,omitempty"` | ||
CapabilityExchange *CapabilityExchangeConfig `json:"capabilityExchange,omitempty"` | ||
} | ||
|
||
type CapabilityExchangeConfig struct { | ||
VendorID *uint32 `json:"vendorID"` | ||
ProductName *string `json:"productName,omitempty"` | ||
OriginHost *string `json:"originHost,omitempty"` | ||
OriginRealm *string `json:"originRealm,omitempty"` | ||
FirmwareRevision *uint32 `json:"firmwareRevision,omitempty"` | ||
HostIPAddresses *[]string `json:"hostIPAddresses,omitempty"` | ||
} | ||
|
||
func processConfig(arg map[string]interface{}) (*DiameterConfig, error) { | ||
|
||
var config DiameterConfig | ||
if b, err := json.Marshal(arg); err != nil { | ||
return nil, err | ||
} else { | ||
if err = json.Unmarshal(b, &config); err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
setDiameterConfigDefaults(&config) | ||
|
||
return &config, nil | ||
} | ||
|
||
func setDiameterConfigDefaults(config *DiameterConfig) { | ||
// Default values | ||
var defaultRequestTimeout = Duration{1 * time.Second} | ||
var defaultMaxRetransmits uint = 1 | ||
var defaultRetransmitInterval = Duration{1 * time.Second} | ||
var defaultEnableWatchdog = true | ||
var defaultWatchdogInterval = Duration{5 * time.Second} | ||
var defaultWatchdogStream uint = 0 | ||
|
||
var defaultVendorID uint32 = 13 | ||
var defaultProductName = "xk6-diameter" | ||
var defaultOriginHost = "origin.host" | ||
var defaultOriginRealm = "origin.realm" | ||
var defaultFirmwareRevision uint32 = 1 | ||
var defaultHostIPAddresses = []string{"127.0.0.1"} | ||
|
||
// Set defaults for DiameterConfig | ||
if config.RequestTimeout == nil { | ||
config.RequestTimeout = &defaultRequestTimeout | ||
} | ||
if config.MaxRetransmits == nil { | ||
config.MaxRetransmits = &defaultMaxRetransmits | ||
} | ||
if config.RetransmitInterval == nil { | ||
config.RetransmitInterval = &defaultRetransmitInterval | ||
} | ||
if config.EnableWatchdog == nil { | ||
config.EnableWatchdog = &defaultEnableWatchdog | ||
} | ||
if config.WatchdogInterval == nil { | ||
config.WatchdogInterval = &defaultWatchdogInterval | ||
} | ||
if config.WatchdogStream == nil { | ||
config.WatchdogStream = &defaultWatchdogStream | ||
} | ||
|
||
// Set defaults for CapabilityExchangeConfig | ||
if config.CapabilityExchange == nil { | ||
config.CapabilityExchange = &CapabilityExchangeConfig{} | ||
} | ||
if config.CapabilityExchange.VendorID == nil { | ||
config.CapabilityExchange.VendorID = &defaultVendorID | ||
} | ||
if config.CapabilityExchange.ProductName == nil { | ||
config.CapabilityExchange.ProductName = &defaultProductName | ||
} | ||
if config.CapabilityExchange.OriginHost == nil { | ||
config.CapabilityExchange.OriginHost = &defaultOriginHost | ||
} | ||
if config.CapabilityExchange.OriginRealm == nil { | ||
config.CapabilityExchange.OriginRealm = &defaultOriginRealm | ||
} | ||
if config.CapabilityExchange.FirmwareRevision == nil { | ||
config.CapabilityExchange.FirmwareRevision = &defaultFirmwareRevision | ||
} | ||
if config.CapabilityExchange.HostIPAddresses == nil { | ||
config.CapabilityExchange.HostIPAddresses = &defaultHostIPAddresses | ||
} | ||
} | ||
|
||
type Duration struct { | ||
time.Duration | ||
} | ||
|
||
func (d Duration) MarshalJSON() ([]byte, error) { | ||
return json.Marshal(d.String()) | ||
} | ||
|
||
func (d *Duration) UnmarshalJSON(b []byte) error { | ||
var v interface{} | ||
if err := json.Unmarshal(b, &v); err != nil { | ||
return err | ||
} | ||
|
||
switch value := v.(type) { | ||
case string: | ||
var err error | ||
d.Duration, err = time.ParseDuration(value) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
default: | ||
return errors.New("invalid duration") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package diameter | ||
|
||
import ( | ||
"github.com/dop251/goja" | ||
"go.k6.io/k6/js/modules" | ||
) | ||
|
||
type ( | ||
Diameter struct { | ||
vu modules.VU | ||
exports *goja.Object | ||
} | ||
RootModule struct{} | ||
Module struct { | ||
*Diameter | ||
} | ||
) | ||
|
||
func init() { | ||
modules.Register("k6/x/diameter", &Diameter{}) | ||
modules.Register("k6/x/diameter/avp", &AVP{}) | ||
modules.Register("k6/x/diameter/dict", &Dict{}) | ||
} |