Custom Search

SUBSCRIBE

SUBSCRIBE : 300 + Free Video Tutorials Programming (Python, golang, Julia), DevOps

Sunday, August 28, 2016

How to fix git diff shows escape characters ^[[32m+^[[m^[[32m

1)
Check all global setting
$ git config --list

2)
Set color.diff=off
$ git config --global color.diff off

3)
Again check all global setting
$ git config --list

4)
Temporary fix
$ git diff --color=never > 1.diff

Thursday, August 25, 2016

openstack tacker how to run unittest

$ python -m testtools.run tackerclient.tests.unit.test_validators.ValidatorTest.test_validate_file_size
$ python -m testtools.run tackerclient.tests.unit.vm.test_cli10_vnf

$ sudo tox -epy27

Sunday, August 21, 2016

Go Golang decode unmarshal json string to struct

package main

import (
"encoding/json"
"fmt"
"reflect"
)

type Person struct {
Name    string
Age     int
Details interface{}
}



func main() {

str := `{"name": "sam",
             "age": 30,
             "details": {"salary":10000}
            }`

/*This means the key of map named data is a string but the value can be anything*/
/*An interface{} type is a type that could be any value. It’s like Object in Java.*/
var data Person

fmt.Println("==type of str==", reflect.TypeOf(str))
fmt.Println("==str==", str)

/*json.Unmarshal is used when the input is []byte*/
/*stores the decoded result in map named data*/
fmt.Println("==[]byte(str)==", []byte(str))
err := json.Unmarshal([]byte(str), &data)

if err != nil {
panic(err)
}

fmt.Printf("==data==%#v \n\n", data)
fmt.Println("==name== ", data.Name)

/*Type assertions, check type of data["details"]
 https://tour.golang.org/methods/15*/
details, ok := data.Details.(map[string]interface{})
if ok {
fmt.Println("==salary== ", details["salary"])
}
}

Go Golang Lowercase key names with JSON Marshal and UnMarshal

package main

/* https://golang.org/pkg/fmt/ */
import (
"encoding/json"
"fmt"
)



/*A struct is a type which contains named fields.
Define new type named 'Person'*/
type Person struct {
Name    string   `json:"name"`
Age     int      `json:"age"`
Hobbies []string `json:"hobbies"`
}

func main() {

/*initialize a struct.
 Create an instance of type 'Person'*/
p1 := Person{"Sam", 20, []string{"cricket", "football"}}
/*p1 := &Person{name: "Sam", age: 20, hobbies: []string{"cricket", "football"}}*/

/*ENCODE*/
/*p1_json_ind, _ := json.MarshalIndent(p1, "", "  ")*/
p1_json, _ := json.Marshal(p1)
fmt.Println("==p1_json==", string(p1_json))

/*DECODE*/
var p2 Person
decode_error := json.Unmarshal(p1_json, &p2)
fmt.Printf("==decode_error==%v \n", decode_error)

fmt.Printf("==p2==%#v \n", p2)

}

Go Golang Pretty Print struct, map, slice, array json.MarshalIndent

package main

/* https://golang.org/pkg/fmt/ */
import (
"encoding/json"
"fmt"
)

/*A struct is a type which contains named fields.
Define new type named 'Person'*/
type Person struct {
Name    string
Age     int
Hobbies []string
}



func main() {

/*initialize a struct.
 Create an instance of type 'Person'*/
p1 := &Person{"Sam", 20, []string{"cricket", "football"}}
/*p1 := &Person{name: "Sam", age: 20, hobbies: []string{"cricket", "football"}}*/

p1_json_ind, _ := json.MarshalIndent(p1, "", "  ")
fmt.Println("==p1_json_ind==", string(p1_json_ind))

/*MAP*/
mapD := map[string]int{"apple": 5, "lettuce": 7}
mapB, _ := json.MarshalIndent(mapD, "", " ")
fmt.Println("==mapD==", string(mapB))

/*SLICE*/
/*https://blog.golang.org/go-slices-usage-and-internals
 https://blog.golang.org/slices*/
slcD := []string{"apple", "peach", "pear"}
slcB, _ := json.MarshalIndent(slcD, "", " ")
fmt.Println("==slice==", string(slcB))
}

Go Golang json.Marshal returns {} when struct field start with lowercase

package main

/* https://golang.org/pkg/fmt/ */
import (
"encoding/json"
"fmt"
)



/*A struct is a type which contains named fields.
Define new type named 'Person'*/
type Person struct {
Name    string
Age     int
Hobbies []string
}

func main() {

/*initialize a struc.
 Create an instance of type 'Person'*/
p1 := &Person{"Sam", 20, []string{"cricket", "football"}}
/*p1 := &Person{name: "Sam", age: 20, hobbies: []string{"cricket", "football"}}*/

p1_json, _ := json.Marshal(p1)
fmt.Println("===p1_json===", string(p1_json))

p1_json_ind, _ := json.MarshalIndent(p1, "", "  ")
fmt.Println("===p1_json_ind===", string(p1_json_ind))

/*With type and field name*/
/* %#v a Go-syntax representation of the value*/
fmt.Printf("=======%#v \n", p1)

}

Friday, August 19, 2016

Go Golang How to pretty print struct variable

1)
package main

/* https://golang.org/pkg/fmt/ */
import (
"fmt"
)

/*A struct is a type which contains named fields.
Define new type named 'Person'*/
type Person struct {
name string
age  int
}

func main() {

/*initialize a struc.
 Create an instance of type 'Person'*/
p := Person{"Sam", 20}

fmt.Println("====", p)

/* %v  print the value in a default format*/
fmt.Printf("=====%v \n", p)

/*With field name*/
fmt.Printf("======%+v \n", p)

/*With type and field name*/
/* %#v a Go-syntax representation of the value*/
fmt.Printf("=======%#v \n", p)
}



2)
output
----------
$go run print_struct.go
==== {Sam 20}
====={Sam 20} 
======{name:Sam age:20} 
=======main.Person{name:"Sam", age:20} 

How to use Sublime Text 2 for Go Golang Development

1)
Goto
"Tools" == "Build System" == "New Build System"

2)
Save the file with name "go.sublime-build"
{
    "cmd": "go run $file",
    "shell" : true
}

* this file will save under /home/your-name/.config/sublime-text-2/Packages/User

3)
Goto
"Tools" == "Build System" == "New Build System"
Then select "go"

4)
Create simple go program in sublime text and select
"Tools" == "Build"


Friday, August 12, 2016

linux how to do bind mount

iscsi LUN is mounted to /netapp

a)
bind mount /netapp to /var/lib/nova/instances
$sudo mount --bind /netapp /var/lib/nova/instances

b)
Find bind mount
$ df -a
/dev/sdb1      1056777312 1160760 1001912260   1% /netapp
/dev/sdb1      1056777312 1160760 1001912260   1% /var/lib/nova/instances

c)

Wednesday, August 10, 2016

neutron server to DHCP agent and DHCP agent to neutron server RPC call example

1)
DHCP agent Service
-----------
a)
client side API:
neutron.agent.dhcp.agent.DhcpPluginAPI
neutron/agent/dhcp/agent.py

b)
Server side API:
neutron.agent.dhcp.agent.DhcpAgent
neutron/agent/dhcp/agent.py

* Here client and server side API classes are defined in same file neutron/agent/dhcp/agent.py

2)
Neutron Server service
-----------
a)
Server side API:
neutron.api.rpc.handlers.dhcp_rpc.DhcpRpcCallback
neutron/api/rpc/handlers/dhcp_rpc.py

b)
client side API:
neutron.api.rpc.agentnotifiers.dhcp_rpc_agent_api.DhcpAgentNotifyApi
neutron/api/rpc/agentnotifiers/dhcp_rpc_agent_api.py

3)
Here:
1a (DhcpPluginAPI) make rpc call to 2a (DhcpRpcCallback)
2b (DhcpAgentNotifyApi) make rpc call to 1b (DhcpAgent).

* DHCP agent Service make rpc call to Neutron Server Service.
* Neutron Server also make rpc call to DHCP agent Service.

Neutron RPC APIs
================

As discussed before, RPC APIs are defined in two parts: a client side and a
server side.  Several of these pairs exist in the Neutron code base.  The code
base is being updated with documentation on every rpc interface implementation
that indicates where the corresponding server or client code is located.

Example: DHCP
-------------

The DHCP agent includes a client API, neutron.agent.dhcp.agent.DhcpPluginAPI.
The DHCP agent uses this class to call remote methods back in the Neutron
server.  The server side is defined in
neutron.api.rpc.handlers.dhcp_rpc.DhcpRpcCallback.  It is up to the Neutron
plugin in use to decide whether the DhcpRpcCallback interface should be
exposed.

Similarly, there is an RPC interface defined that allows the Neutron plugin to
remotely invoke methods in the DHCP agent.  The client side is defined in
neutron.api.rpc.agentnotifiers.dhcp_rpc_agent_api.DhcpAgentNotifyApi.  Th
server side of this interface that runs in the DHCP agent is
neutron.agent.dhcp.agent.DhcpAgent.

--------------



Friday, August 5, 2016

openstack tacker toscaparser and hot tosca_translator example

import toscaparser
from toscaparser.utils import yamlparser
from toscaparser.tosca_template import ToscaTemplate
from translator.hot.tosca_translator import TOSCATranslator
from tacker.vm.tosca import utils as toscautils

vnfd_yaml = open("sample.yaml")
vnfd_dict = yamlparser.simple_ordered_parse(vnfd_yaml)

toscautils.updateimports(vnfd_dict)

tosca = ToscaTemplate(yaml_dict_tpl=vnfd_dict)


monitoring_dict = toscautils.get_vdu_monitoring(tosca)
mgmt_ports = toscautils.get_mgmt_ports(tosca)
res_tpl = toscautils.get_resources_dict(tosca)

toscautils.post_process_template(tosca)


translate = TOSCATranslator(tosca, {})
heat_template_yaml = translate.translate()


heat_template_yaml = toscautils.post_process_heat_template(heat_template_yaml, mgmt_ports, res_tpl)

print heat_template_yaml