Skip to content
This repository has been archived by the owner on Jan 11, 2023. It is now read-only.

Commit

Permalink
add unmarshal function for 2017-07-01, with TODO comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Jingtao Ren committed Jul 8, 2017
1 parent 117b4e3 commit 9384fc5
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
50 changes: 50 additions & 0 deletions pkg/api/v20170701/types.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package v20170701

import (
"encoding/json"
"fmt"
"strings"
)
Expand Down Expand Up @@ -123,6 +124,27 @@ type MasterProfile struct {
FQDN string `json:"fqdn,omitempty"`
}

// UnmarshalJSON unmarshal json using the default behavior
// And do fields manipulation, such as populating default value
func (m *MasterProfile) UnmarshalJSON(b []byte) error {
// Need to have a alias type to avoid circular unmarshal
type aliasMasterProfile MasterProfile
mm := aliasMasterProfile{}
if e := json.Unmarshal(b, &mm); e != nil {
return e
}
*m = MasterProfile(mm)
if m.Count == 0 {
// if MasterProfile.Count is missing or 0, set to default 1
m.Count = 1
}

// TODO:
// Need to set default values for
// OSDiskSizeGB, FirstConsecutiveStaticIP, StorageProfile
return nil
}

// AgentPoolProfile represents configuration of VMs running agent
// daemons that register with the master and offer resources to
// host applications in containers.
Expand All @@ -146,6 +168,34 @@ type AgentPoolProfile struct {
subnet string
}

// UnmarshalJSON unmarshal json using the default behavior
// And do fields manipulation, such as populating default value
func (a *AgentPoolProfile) UnmarshalJSON(b []byte) error {
// Need to have a alias type to avoid circular unmarshal
type aliasAgentPoolProfile AgentPoolProfile
aa := aliasAgentPoolProfile{}
if e := json.Unmarshal(b, &aa); e != nil {
return e
}
*a = AgentPoolProfile(aa)
if a.Count == 0 {
// if AgentPoolProfile.Count is missing or 0, set it to default 1
a.Count = 1
}

if string(a.OSType) == "" {
// OSType is the operating system type for agents
// Set as nullable to support backward compat because
// this property was added later.
// If the value is null or not set, it defaulted to Linux.
a.OSType = Linux
}
// TODO:
// Need to set default values for
// OSDiskSizeGB, StorageProfile
return nil
}

// OrchestratorType defines orchestrators supported by ACS
type OrchestratorType string

Expand Down
35 changes: 35 additions & 0 deletions pkg/api/v20170701/types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package v20170701

import (
"encoding/json"
"testing"
)

func TestMasterProfile(t *testing.T) {
MasterProfileText := "{\"count\" : 0}"
mp := &MasterProfile{}
if e := json.Unmarshal([]byte(MasterProfileText), mp); e != nil {
t.Fatalf("unexpectedly detected unmarshal failure for MasterProfile, %+v", e)
}

if mp.Count != 1 {
t.Fatalf("unexpectedly detected MasterProfile.Count != 1 after unmarshal")
}
}

func TestAgentPoolProfile(t *testing.T) {
// With osType not specified
AgentPoolProfileText := "{\"count\" : 0}"
ap := &AgentPoolProfile{}
if e := json.Unmarshal([]byte(AgentPoolProfileText), ap); e != nil {
t.Fatalf("unexpectedly detected unmarshal failure for AgentPoolProfile, %+v", e)
}

if ap.Count != 1 {
t.Fatalf("unexpectedly detected AgentPoolProfile.Count != 1 after unmarshal")
}

if ap.OSType != Linux {
t.Fatalf("unexpectedly detected AgentPoolProfile.OSType != Linux after unmarshal")
}
}

0 comments on commit 9384fc5

Please sign in to comment.